Tuesday, December 29, 2015

NoClassDefFoundError exception in your Maven project

After you have added a new dependency, you could have exception like this:

NetBeans error:

--- exec-maven-plugin:1.2.1:exec (default-cli) @ integration ---
java.lang.NoClassDefFoundError: path/to/the/class/ClassName
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: package.ClassName
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

IntellijIdea error:

JAXB: Marshalling kickstart blank template application

This is a simple JAXB application, that could be used as template. It has console (and file) output.

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAttribute;

@XmlRootElement
public class Test {
    String name;
    int age;
    int id;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }



    public static void main(String[] args) {

        Test test = new Test();
        test.setId(100);
        test.setName("Tim Rott");
        test.setAge(29);

        try {

//            File file = new File("C:\\file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

//            jaxbMarshaller.marshal(test, file);
            jaxbMarshaller.marshal(test, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }
}

JAXB: Generate schema and generate Java class

You could easily generate .xsd file from java model class and vice versa.

Generate the schema

Lets have a model class as follows:

@XmlRootElement(name="product")
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {

    @XmlElement(required=true) 
    protected int id;
    @XmlElement(required=true) 
    protected String name;
    @XmlElement(required=true) 
    protected String description;
    @XmlElement(required=true) 
    protected int price;
    
    public Product() {}
    
    // Getter and setter methods
    // ...
}
 
Run the JAXB schema generator on the command line to generate the corresponding XML schema definition:

schemagen Product.java

This command produces the XML schema as an .xsd file.

Generate Java class

Lets have schema definition in .xsd file:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://xml.product" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"    
           elementFormDefault="qualified"
           xmlns:myco="http://xml.product">

  <xs:element name="product" type="myco:Product"/>

  <xs:complexType name="Product">
    <xs:sequence>
      <xs:element name="id" type="xs:int"/>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="description" type="xs:string"/>
      <xs:element name="price" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
 
Run the schema compiler tool on the command line as follows:

xjc Product.xsd
 
This will produce the following class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Product", propOrder = {
    "id",
    "name",
    "description",
    "price"
})
public class Product {
    protected int id;
    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected String description;
    protected int price;

    // Setter and getter methods
    // ...
}

Source

Source: https://docs.oracle.com/javaee/6/tutorial/doc/gkknj.html

Monday, December 21, 2015

IF operator alternative in JPQL

JPQL (and dialects as HQL) does not support IF operator/function, but the same functionality could be built with CASE construction:

CASE
    WHEN condition_1 THEN
         ...
    [WHEN condition_n THEN]
         ...
    ELSE
         ... 
END

Example:
SELECT e.name, 
  CASE 
    WHEN (e.salary &gt;= 100000) THEN 1 
    WHEN (e.salary &lt; 100000) THEN 2 
    ELSE 0  
  END 
FROM Employee e;

Monday, December 14, 2015

MySQL Data Engines comparison - MyIsam Vs Innodb Vs Memory

Feature MyISAM InnoDB Memory
ACID Transaction
ACID - Atomicity, Consistency, Isolation, Durability (read more on it here: http://en.wikipedia.org/wiki/ACID)
No Yes No
Configurable ACID Properties No Yes No
Crash Safe No Yes No (RAM)
Foreign Key Support No Yes No
Multi Version Concurrency Control (MVCC) No Yes No
Geospatial datatype Yes Yes No
Geospatial indexing Yes No No
Full-text Search Index Yes No No
Data Cache No Yes N/A
Compressed Data Yes Yes No
Storage Limits 256TB 64TB RAM
Storage Cost Low High N/A
Memory Cost Low High Medium
Locking Granularity Table Row Table


MyISAM:
The MyISAM storage engine in MySQL.
  • Simpler to design and create, thus better for beginners. No worries about the foreign relationships between tables.

A Windows editor for huge files - PilotEdit

PilotEdit is nice Windows editor for huge files.

There is two versions:
 - normal (paid with trial) and 
 - lite (free)

http://www.pilotedit.com/index.php?option=com_content&view=section&layout=blog&id=8&Itemid=56

Thursday, December 10, 2015

Get extended EXPLAIN profiling data in MySQL

As always with a slow query, finding the execution plan with EXPLAIN is the 1st step to understand where time is spent.

For extended profiling information, SHOW PROFILE can be used: 


mysql> set @@profiling = 1;
mysql> SELECT ....;
mysql> SHOW PROFILE;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| starting           | 0.000783 |
| Opening tables     | 0.000192 |
| System lock        | 0.000005 |
| Table lock         | 0.000010 |
| init               | 0.000369 |
| optimizing         | 0.000091 |
| statistics         | 3.459529 |
| preparing          | 0.000213 |
| executing          | 0.000005 |
| Sending data       | 0.004597 |
| end                | 0.000008 |
| query end          | 0.000005 |
| freeing items      | 0.000066 |
| logging slow query | 0.000004 |
| cleaning up        | 0.000006 |
+--------------------+----------+



Source: https://www.percona.com/blog/2014/03/06/many-table-joins-mysql-5-6/

Thursday, December 3, 2015

JPA Cache behavior with JPQL Update and Delete queries

JPA experience desynchronization between read data and cache while executing modifying queries. Even in the same application. It is helpful to know JPA behavior to avoid cache issues.

In short

Update queries: Lets have an update statement, which will update all rows in the database (no where cause). It will be executed on the database itself only - updating first level cache and second level cache will not be executed. Next time you access some row via that cache, you will get the old value (if the cache has not been invalidated because of timeout).

Delete queries: deleting all rows with JPQL will result removing them from database but they will stay in both caches.

1st Level Cache

If you bypass JPA and execute DML directly on the database, either through native SQL queries, JDBC, or JPQL UPDATE or DELETE queries, then the database can be out of synch with the 1st level cache. If you had accessed objects before executing the DML, they will have the old state and not include the changes. Depending on what you are doing this may be ok, otherwise you may want to refresh the affected objects from the database.

The 1st level, or EntityManager cache can also span transaction boundaries in JPA. A JTA managed EntityManager will only exist for the duration of the JTA transaction in JEE. Typically the JEE server will inject the application with a proxy to an EntityManager, and after each JTA transaction a new EntityManager will be created automatically or the EntityManager will be cleared, clearing the 1st level cache. In an application managed EntityManager, the 1st level cache will exist for the duration of the EntityManager. This can lead to stale data, or even memory leaks and poor performance if the EntityManager is held too long. This is why it is generally a good idea to create a new EntityManager per request, or per transaction. The 1st level cache can also be cleared using the EntityManager.clear() method, or an object can be refreshed using the EntityManager.refresh() method.

2nd Level Cache

 If the application is the only application and server accessing the database there is little issue with the 2nd level cache, as it should always be up to date. The only issue is with DML, if the application executes DML directly to the database through native SQL queries, JDBC, or JPQL UPDATE or DELETE queries. JPQL queries should automatically invalidate the 2nd level cache, but this may depend on the JPA provider. If you use native DML queries or JDBC directly, you may need to invalidate, refresh or clear the objects affected by the DML.

If there are other applications, or other application servers accessing the same database, then stale data can become a bigger issue. Read-only objects, and inserting new objects should not be an issue. New objects should get picked up by other servers even when using caching as queries typically still access the database. It is normally only find() operations and relationships that hit the cache. Updated and deleted objects by other applications or servers can cause the 2nd level cache to become stale.

For deleted objects, the only issue is with find() operations, as queries that access the database will not return the deleted objects. A find() by the object's Id could return the object if it is cached, even if it does not exist. This could lead to constraint issues if you add relations to this object from other objects, or failed updates, if you try to update the object. Note that these can both occur without caching, even with a single application and server accessing the database. During a transaction, another user of the application could always delete the object being used by another transaction, and the second transaction will fail in the same way. The difference is the potential for this concurrency issue to occur increases.

For updated objects, any query for the objects can return stale data. This can trigger optimistic lock exceptions on updates, or cause one user to overwrite another user's changes if not using locking. Again note that these can both occur without caching, even with a single application and server accessing the database. This is why it is normally always important to use optimistic locking. Stale data could also be returned to the user.

Refreshing

Refreshing is the most common solution to stale data. Most application users are familiar with the concept of a cache, and know when they need fresh data and are willing to click a refresh button. This is very common in an Internet browser, most browsers have a cache of web pages that have been accessed, and will avoid loading the same page twice, unless the user clicks the refresh button. This same concept can be used in building JPA applications. JPA provides several refreshing options, see refreshing.

Some JPA providers also support refreshing options in their 2nd level cache. One option is to always refresh on any query to the database. This means that find() operations will still access the cache, but if the query accesses the database and brings back data, the 2nd level cache will be refreshed with the data. This avoids queries returning stale data, but means there will be less benefit from caching. The cost is not just in refreshing the objects, but in refreshing their relationships. Some JPA providers support this option in combination with optimistic locking. If the version value in the row from the database is newer than the version value from the object in the cache, then the object is refreshed as it is stale, otherwise the cache value is returned. This option provides optimal caching, and avoids stale data on queries. However objects returned through find() or through relationships can still be stale. Some JPA providers also allow find() operation to be configured to first check the database, but this general defeats the purpose of caching, so you are better off not using a 2nd level cache at all. If you want to use a 2nd level cache, then you must have some level of tolerance to stale data.

Source: https://en.wikibooks.org/wiki/Java_Persistence/Caching

Friday, November 27, 2015

MSDOS applications run with pause on exit

I have a shortcut to a .BAT file (or any kind of DOS application). I need after its execution, DOS window not to close until i read the output of the program(s).

Adding a line with pause in the end of file is not an option (my file stays in VCS).

Solution: You could change the command of shortcut from this:

c:\path\shortcut.bat

to this:

c:\path\shortcut.bat & pause

You will get an message "Press any key to continue" and you will read the output of the program.

Thursday, November 26, 2015

Best shortcuts for IntelliJ Idea

Open class by name Ctrl + N
Show members Ctrl+F12
Go to implementation Ctrl + Alt + B
Open file by name Ctrl + Shift + N
Jump to next/prev cursor position Alt + left/right arrow
Show in Project Alt + F1 + Enter

Wednesday, November 25, 2015

Install Java JDK/JRE on Debian jessie with 'apt-get install'

Issue: default JDK for Debian 8 is JDK 7. I need a maintainable way to upgrade it to JDK 8.

Steps:

1. Edin file /etc/apt/sources.list and add this line on the bottom:
deb http://http.debian.net/debian jessie-backports main

2. sudo apt-get update

3. sudo apt-get install openjdk-8-jdk

4. test it as this:
java -version

If the result shows version 7, you could check the names of JDK7 packages and remove them
4.1. Check older JDK packages:
dpkg -l | grep openjdk

4.2. Remove your older JDK packages, e.g.:
apt-get purge openjdk-7-jdk
apt-get purge openjdk-7-jre
apt-get purge openjdk-7-jre-headless



Solved.

Tuesday, November 24, 2015

JAMon - nice and simple java monitor for measuring code performance in Java

General information

Official site: http://jamonapi.sourceforge.net/
A SourceForge project

Maven dependency

Place in pom.xml:
    <dependencies>
        <dependency>
            <groupId>com.jamonapi</groupId>
            <artifactId>jamon</artifactId>
            <version>2.81</version>
        </dependency>
    </dependencies>

Example 1

Code:

import com.jamonapi.*;


public class MonitorTest {
    public static void main(String[] args) throws Exception {
        Monitor mon=null;
        for (int i=1; i<=10; i++) {
            mon = MonitorFactory.start("myFirstMonitor");
            Thread.sleep(100+i);
            mon.stop();
        }
        System.out.println(mon);  // toString() method called
    }
}
Output:

Example 2

Tuesday, November 10, 2015

Размишления за DDR паметта - DDR1, DDR2, DDR3, DDR4

Базова честота и ефективна честота

DDR паметта (ddr1, ddr2, ddr3, ddr4) има базова честота и ефективна честота. Ефективната честота е двойно по-голяма от базовата. При ddr2-800, базовата е 400MHz. Това са теоретично 3200 МБ/с. Но при DDR2-800 се постигат теоретични 6400 МБ/с.

От къде идва удвояването? 

При по-простия подход (SDR) паметта предава 1 бит информация за един пълен цикъл. При DDR паметта може да се предадат два бита информация.
Формулата за пропусквателната способност е следната:

пропусквателната способност = реална честота * ширина на шината в битове * брой пренасяни битове за цикъл

Например за DDR2-800 имаме 400 MHz * 64 бита * 2 бита; 64 бита са 8 байта. Това са ефективни 6400МБ/с, което е ефективни 800 MHz. Това важи за всяка памет DDR2-800 и е аналогично за останалите честоти.

Неофициално максимална памет

За IBM/Lenovo ThinkPad - http://www.thinkwiki.org/wiki/Unofficial_maximum_memory_specs

Продажба

Тук може да намерите евтина DDR2 DDR2-800 памет за AMD процесори на добра цена.
Разширена съвместимост с:
Asus M2N-E
Asus m4n78 Pro - с последен BIOS се поддържат 4 GB-тови модули, но само 2. Ако скожите 3 или 4, почва да дава проблеми, все едно модулите или слотовете са дефектни.

Thursday, November 5, 2015

Java: Poor performance of Files.newDirectoryStream() with wildcards

Here are results of comparison of getting single result of Files.newDirectoryStream() and File.exists().

Wildcards pattern I used is "prefix_prefix_prefix____?.tmp". Results are almost the same when calling Files.newDirectoryStream() with string argument with no wildcard (direct match).

Test results 1:

measure exists speed in loop 1000 times - BEGIN
measure exists speed in loop 1000 times - END
Elapsed: 5 ms

Test results 2:

measure wildcards match speed in loop 1000 times - BEGIN
measure wildcards match speed in loop 1000 times - END
Elapsed: 80590 ms

Test environment:

Windows 7 system
NTFS system
Folder with 90 000 files (zero length)
Java 8

dir approach

I looped this MSDOS command for 1000 times to check performance of dir with wildcards:

dir prefix_prefix_prefix____*.tmp

I tested it in loop with:
FOR /L %i IN (1,1,1000) DO @dir prefix_prefix_prefix____*.tmp > nul

It finished in about 1000ms. I suppose this time is spend mainly in calling DIR command by command interpreter.

Conclusion

Files.newDirectoryStream() has absolutely no performance optimizations when working with wildcards.

Relative information

File.list also has poor performance

Source code of newDirectoryStream

    public static DirectoryStream<Path> newDirectoryStream(Path dir, String glob)
        throws IOException
    {
        // avoid creating a matcher if all entries are required.
        if (glob.equals("*"))
            return newDirectoryStream(dir);

        // create a matcher and return a filter that uses it.
        FileSystem fs = dir.getFileSystem();
        final PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
        DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
            @Override
            public boolean accept(Path entry)  {
                return matcher.matches(entry.getFileName());
            }
        };
        return fs.provider().newDirectoryStream(dir, filter);
    }

Tuesday, September 15, 2015

Solution for Jooma: "Error loading component: com_tags, Component not found"

"Error loading component: com_tags, Component not found" came after I updated manually Joomla.

To solve the issue check these steps:

1. Ensure folder /administrator/components/com_tags exists on the server. I have 5 folders and 5 files there.

If you have not this folder, upload it from joomla installation.

2. Check table *_extensions (v1_extensions) for element with name "mod_tags".

If you have not, execute this code (you could change the table name xvm7w_extensions on your _extensions table and extension_id on the first unassigned value):

INSERT INTO `xvm7w_extensions` (`extension_id`, `name`, `type`, `element`, `folder`, `client_id`, `enabled`, `access`, `protected`, `manifest_cache`, `params`, `custom_data`, `system_data`, `checked_out`, `checked_out_time`, `ordering`, `state`) VALUES (29, 'com_tags', 'component', 'com_tags', '', 1, 1, 1, 1, '{"legacy":false,"name":"com_
tags","type":"component","creationDate":"March 2013","author":"Joomla! Project","copyright":"(C) 2005 - 2014 Open Source Matters. All rights reserved.","authorEmail":"admin@joomla.org","authorUrl":"www.joomla.org","version":"3.0.0","description":"COM_TAGS_XML_DESCRIPTION","group":""}', '{}', '', '', 0, '0000-00-00 00:00:00', 0, 0), (316, 'mod_tags_popular', 'module', 'mod_tags_popular', '', 0, 1, 1, 0, '{"name":"mod_tags_popular","type":"module","creationDate":"January 2013","author":"Joomla! Project","copyright":"Copyright (C) 2005 - 2014 Open Source Matters. All rights reserved.","authorEmail":"admin@joomla.org","authorUrl":"www.joomla.org","version":"3.1.0","description":"MOD_TAGS_POPULAR_XML_DESCRIPTION","group":""}', '{"maximum":"5","timeframe":"alltime","owncache":"1"}', '', '', 0, '0000-00-00 00:00:00', 0, 0), (317, 'mod_tags_similar', 'module', 'mod_tags_similar', '', 0, 1, 1, 0, '{"name":"mod_tags_similar","type":"module","creationDate":"January 2013","author":"Joomla! Project","copyright":"Copyright (C) 2005 - 2014 Open Source Matters. All rights reserved.","authorEmail":"admin@joomla.org","authorUrl":"www.joomla.org","version":"3.1.0","description":"MOD_TAGS_SIMILAR_XML_DESCRIPTION","group":""}', '{"maximum":"5","matchtype":"any","owncache":"1"}', '', '', 0, '0000-00-00 00:00:00', 0, 0), (447, 'plg_finder_tags', 'plugin', 'tags', 'finder', 0, 1, 1, 0, '{"name":"plg_finder_tags","type":"plugin","creationDate":"February 2013","author":"Joomla! Project","copyright":"(C) 2005 - 2014 Open Source Matters. All rights reserved.","authorEmail":"admin@joomla.org","authorUrl":"www.joomla.org","version":"3.0.0","description":"PLG_FINDER_TAGS_XML_DESCRIPTION","group":""}', '{}', '', '', 0, '0000-00-00 00:00:00', 0, 0);

Wednesday, September 9, 2015

Hidden AVG buttons - How to stay on AVG Free edition

AVG is great antivirus software. It has free and paid editions. But Free version often offers you an update to the paid one. You have to read notifications of the program, but look for the "hidden" Decline button.

Here are some example of this hidden buttons of AVG:



Good luck.

And the free version is good enough for most users.

Friday, September 4, 2015

AMD E-450 drivers for Windows XP - official links

In this article I post drivers for AMD E-450 CPU (APU) video, audio and chipset drivers for Windows XP, because it is removed from site of AMD/ATI.
The drivers suggested on that page are buggy. For example when the laptop gets into sleep state, it could not wake up and it has to be restarted. It is the same for hibernate... This means unusable. Shame on AMD. May be installing Win 7+ is the only solution...

The first link is from Lenovo site so you could thrust that distribution. The distribution is marked as "For Lenovo notebooks", but you could use on any kind/make of notebook. I tested this on HP notebook and it works fine.

You could download the drivers from this Lenovo link.
You could download another drivers from elitegroup website.
You could download another drivers from biostar website.

It seems biostar has newest version of AMD software.

The drivers are for:
  • AMD Radeon HD 6310 Graphics (E350/E300/E240 APU)
  • AMD Radeon HD 6320 Graphics (E450 APU)
  • AMD Radeon HD 6200 series Graphics (C50 APU)
The supported OS versions are:
  • Windows XP 32 bit edition
  • Windows XP 64 bit edition
If you experience unexpected error during installation, you could install Microsoft .Net framework version 2 and try again.

Sunday, August 16, 2015

Redirect http page to https in .htaccess file

You could use this code to redirect a request to HTTPS in apache:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Tuesday, March 10, 2015

Програмистки поговорки

Програмистки хумор:
- Бърза програма - срам за програмиста.
- Трай, потребителю, за работещо приложение.
- Да би мирно седяло, не би exception видяло!
- Бъг година храни.
- Програмистът работата си мени, но хигиената - никога!
- Когато клиентът не отива при програмиста, програмистът му пуска вирус.
- Дваж copy-рай, веднъж paste-вай.
- Бъг бъг избива.
- Програмистът не пада по-далеч от кръчмата.
- Който прави бъг другиму, сам влиза в безкраен цикъл.
- На сисадмин вируси ще продава!
- На програмата паметта все й е малко.
- Бъг по бъг - програма прави.
- Не търси в кода смисъл!
- За тийм лидера приказват, а пък той под масата.
- Барабар junior със senior-ите.
- Признат бъг - половин бъг.
- Не дърпай сисадмина за опашката!
- Ако е тийм лидер, да е рошав!
- Шеф високо, клиент далеко.

- На чужд код и 100 рефакторинга са малко.

Други забавни смешки може да намерите тук.

Wednesday, January 28, 2015

Artisteer cannot create localized sites - no localized headline nor slogan

Artisteer, a template generator for joomla cannot create localized headlines and slogans. This was a comment from official forum.

What a joke!!! What a limitation!!! By now, 12 days passed and I have not answer on question about how could I localize them. What if this was my blocker for me? I cannot realize that noone could answer this SIMPLE question.

Artiseer - what a joke! I will have a look in its alternative - Template Toaster. It seems that such a promoted software (Artisteer) has so much limitations at first sight...