Kategori: Java

”The libs.CopyLibs.classpath property is not set up.”

Importing a Java Enterprise Application (.ear) project in Netbeans 8.2 from Existing Sources, ant will sometimes erroneously end up not finding CopyLibs in the classpath.

To solve it, you simply double-click (open as separate projects) each and every module in the Java Enterprise Application.

Using MySQL Connector/J (JDBC Driver) with Glassfish

Prerequisites:

1. Download JDBC driver

At the moment of writing, the latest version (8.0.15) of the JDBC driver does not work with Glassfish. Use 5.1.47 instead.

For the lazy, go to your glassfish domain folder

\glassfish\domains\domain1\lib\

and run the following:

wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.47.zip

jar xf mysql-connector-java-5.1.47.zip

move mysql-connector-java-5.1.47\mysql-connector-java-5.1.47.jar .

 

2. Set up JDBC Connection Pool

Glassfish Admin Console:

Resources > JDBC > JDBC Connection Pools  > New

Configure as follows:

Pool Settings and Transaction is up to you

In Properties, the following are required:

User: {your db user}

Password: {your db user password}

URL: jdbc:mysql://{host}:{port}/{database name}

Url: jdbc:mysql://{host}:{port}/{database name}

Click ”Finish”

If everything is set up correctly, you should see this:

 

3. Set up the JDBC Resource

Glassfish Admin Console:

 Resources > JDBC > JDBC Resources > New

Configure as follows:

Remember your JNDI Name

Click ”Save”

4. Use the JDBC Resource

Sample code to connect and start querying:

InitialContext ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("jdbc/DemoJNDI");
        
    conn = (Connection) ds.getConnection();
    
    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT 1");

        rs = stmt.getResultSet();
        while (rs.next()) {
        // Do something with ResultSet    
        }
    } finally {
        conn.close(); 
    }

Done!

JavaFX-Project in Netbeans: Compile on Save

In some projects, JavaFX being one of them, NetBeans will force you to do manual ”Clean and Build” to compile instead of simply having it compile on save. Changing this property from the GUI (Project > Properties) doesn’t always seem to be possible. The solution is simple – in your project.properties, change this

compile.on.save.unsupported.javafx=true

to

compile.on.save.unsupported.javafx=false

project.properties is located in /[Projectfolder]/nbproject

Detecting uses of @Deprecated (Java version compatability)

The most straightforward way to find invocations of @Deprecated classes/methods in a given .jar file is the standalone CLI-tool jdeprscan that comes with Java 9.

While it is technically possible to use @Deprecated classes, it is highly discouraged. Especially if the the classes are tagged with ”for-removal”, which jdeprscan can detect aswell.

At best, a @Deprecated class/method may be due to a small spelling error. At its worst, using a deprecated class/method may result in unpredictable and hard-to-detect errors.

 

To scan a .jar file for uses of @Deprecated, simply do:

jdeprscan myjar.jar

 

 

This will check the .jar against the latest installed release in your path. For specific releases, you can use the option –release.

To check for-removal, simply do:

jdeprscan myjar.jar --for-removal

 

 

Full documentation here