I'm porting a project to Maven. It seems that I'm almost done, though there's still a strange problem. I have an enum:
package cz.autoclient.settings;
public enum Setnames {
SETTING1("s1", false),
SETTING2("s2", 666),
;
public final String name;
public final Object def;
Setnames(String n, Object d) {
name = n;
def = d;
}
}
I use this particular enum to avoid re-creation of String whenever some setting is loaded from the database. And to store default values.
In my old project, this was valid:
import cz.autoclient.settings.Setnames;
In Maven, there's a problem:
Exception in thread "main" java.lang.NoClassDefFoundError: cz/autoclient/settings/Setnames
at cz.autoclient.Gui.createTabs(Gui.java:326)
at cz.autoclient.Gui.initComponents(Gui.java:165)
at cz.autoclient.Gui.<init>(Gui.java:58)
at cz.autoclient.Main.startGUI(Main.java:71)
at cz.autoclient.Main.<init>(Main.java:32)
at cz.autoclient.Main.main(Main.java:98)
Caused by: java.lang.ClassNotFoundException: cz.autoclient.settings.Setnames
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
Gui.java:326 is the line where I first use Setnames. I've been googling and what I got that you've got to use $ when naming enums somehow.
But I've no idea where should I put that $ in my case - other people allways had this problem when enum was hidden within a class.
Here's what it looks like in my IDE:
Here's the project pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cz.autoclient</groupId>
<artifactId>autoclient</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<src.dir>src/</src.dir>
<test.dir>test/</test.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<testSourceDirectory>${test.dir}</testSourceDirectory>
<sourceDirectory>${src.dir}</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<name>Auto Client</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>4.1.0</version>
</dependency>
</dependencies>
</project>
This is the command line NetBeans is using to compile the project:
cd C:\MYSELF\programing\java\AutoCall\AutoClient; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_31" cmd /c "\"\"C:\\Users\\Jakub\\AppData\\Roaming\\NetBeans\\7.4\\maven\\bin\\mvn.bat\" -Dexec.args=\"-classpath %classpath cz.autoclient.Main\" -Dexec.executable=\"C:\\Program Files\\Java\\jdk1.8.0_31\\bin\\java.exe\" -DnetbeansProjectMappings= -Dmaven.ext.class.path=C:\\Users\\Jakub\\AppData\\Roaming\\NetBeans\\7.4\\maven-nblib\\netbeans-eventspy.jar org.codehaus.mojo:exec-maven-plugin:1.2.1:exec\""
You created a maven project and moved your class from cz.autoclient.settings.Setnames to cz.autoclient.PVP_net.Setnames. Now you're getting an NoClassDefFoundError which means that java simply can't find your class. Why? Because it's moved to another place, but this has nothing to do with maven it has just happened after creating a maven project.
Please check your whole project for imports like:
import cz.autoclient.settings.Setnames;
an replace them by something like:
import cz.autoclient.PVP_net.Setnames;
I hope that helps you.
Turned out it was some kind of glitch in the project. I have no idea how it happened.
Refactoring the packages with problematic classes fixed the problem.
Related
I used the following basic Maven command to generate a project:
mvn archetype:generate -DgroupId=it.maven.project -DartifactId=MavenExample -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
The project was correctly created and I could test the automatically generated App class without any problem with this instruction:
java -cp target/MavenExample-1.0-SNAPSHOT.jar:lib/* it.maven.project.App
Later on, I added some dependencies to the POM, obtaining the following file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.maven.project</groupId>
<artifactId>MavenExample</artifactId>
<version>1.0-SNAPSHOT</version>
<name>MavenExample</name>
<!-- FIXME change it to the project's website -->
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Finally, for testing sake, I modified the App class previously generated by Maven itself:
package it.maven.project;
import org.apache.commons.lang3.RandomStringUtils;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase());
}
}
A series of strange things happen:
even though more recent versions of dependencies are specified in POM, in .m2 folder the downloaded versions seem to be older (for instance I get 2.1 and 2.5 for commons-lang3)
the project is correctly compiled by instruction
mvn clean install -U
when I run again command to execute App, I get the following exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/RandomStringUtils
at it.maven.project.App.main(App.java:13)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.RandomStringUtils
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
Questions:
how is it possible that project compiles but then errors are returned at compiling time? Why the import in App is accepted but instruction:
System.out.println("Stringa generata casualmente: " + RandomStringUtils.random(16, true, true).toUpperCase());
generates an exception?
what should I correct/execute to allow my program to work and use correctly dependencies?
when you create a simple java project you define a set of dependencies in the pom but if you don't create a java archive file (ejb-jar or war) all the dependencies are not available at runtime if you run the jar compiled in the target directory.
There are two solutions:
Create a uber jar that include all your depenencies in your jar: use stackoverflow solution
when you run the jar from command line you have to add the dependencies to the classpath:
java -cp "/path/dependencies/dep1.jar;/path/dependencies/dep2.jar" -jar myApp.jar
I'm trying to install the Deeplearning4j library ( https://deeplearning4j.org/index.html) but I don't understand how to use install the lib correctly with IntelliJ and Maven so that I can build a .jar file from it.
As long as I'm running the program from IntelliJ everything seems to work.
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>DeepLearning</groupId>
<artifactId>deeplearning</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.deeplearning4j/deeplearning4j-core -->
<dependency>
<groupId>org.deeplearning4j</groupId>
<artifactId>deeplearning4j-core</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.nd4j/nd4j-native -->
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.nd4j/nd4j-api -->
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-api</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.nd4j/nd4j-native-platform -->
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>0.9.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.datavec/datavec-api -->
<dependency>
<groupId>org.datavec</groupId>
<artifactId>datavec-api</artifactId>
<version>0.9.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>MLPClassifierLinear</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I am not sure if everything is set up correctly because it's the first time I use maven.
When I run the maven install command and start the .jar file I get an error that says that a JNI error has occurred and a NoClassDefFoundError.
This is the exact error message:
Error: A JNI error has occurred, please check your installation and
try again
Exception in thread "main" java.lang.NoClassDefFoundError:
org/deeplearning4j/nn/conf/layers/Layer 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:
org.deeplearning4j.nn.conf.layers.Layer 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
Can somebody explain me how to use maven correctly so I can build .jar files without getting errors?
Thank you :)
Simple recipe for getting most things done:
deeplearning4j-core, nd4j-native-platform, maven shade plugin
deeplearning4j-core gives you most dependencies people use on simple desktop.
nd4j-native-platform bundles all operating system native dependencies so you don't have to worry about multi OS deployments/binaries. This also makes sbt and gradle actually..usable since they can't handle classifiers.
maven shade plugin handles building the jar properly.
https://github.com/deeplearning4j/dl4j-examples/blob/94568e78e86c56807c03fe17d6a2f89f0b0df377/dl4j-spark-examples/dl4j-spark/pom.xml#L98
Also, please don't use the term "install". You aren't installing anything. You're setting up a set of libraries using a dependency manager. It's not installing in the sense of the OS like ruby and python tend to do.
Beyond that: Specific critiques about your pom. nd4j-native here is redundant. You don't need that. You only use nd4j-native if you are using snapshots or building from source.
Datavec-api isn't needed because it's already brought in by deeplearning4j-core. Please read up on maven transitive dependencies to understand how this works.
If you aren't sure on how any of these things get resolved, look at using
mvn dependency:tree
I have an Maven Eclipse project and it runs fine within Eclipse, however if I attempt to run it from the command line it fails to find the dependencies.
The pom.xml file:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.something.or.rather.myapp</groupId>
<artifactId>MyApp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>MyApp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cloudant</groupId>
<artifactId>cloudant-client</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
From the project directory (MyApp):
mvn eclipse:eclipse // runs fine
mvn package // runs fine
java -cp target/MyApp-1.0-SNAPSHOT.jar com.something.or.rather.myapp.MyMainClass // fails
...
Exception in thread "main" java.lang.NoClassDefFoundError: com/cloudant/client/api/CloudantClient
...
I notice if I put the full path of the Cloudant JAR file in the java command it resolves that exception only to raise another.
java -cp /full/path/to/cloudant-client-1.0.1jar:target/MyApp-1.0-SNAPSHOT.jar com.something.or.rather.myapp.MyMainClass // still fails
...
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/methods/HttpRequestBase
...
Any ideas? This is painful.
By default maven builds thin jars without the project dependencies, only the project classes.
What you want is a fat jar. To do this have a look at:Building a fat jar using maven
Hope this helps
Currently my NetBeans 8.0 has been doing the strangest thing.
I have a multimodule project. Usually I did not even have to have the projects opened or even downloaded, if the modules were in maven repository. Currently, some of my modules however need to be opened and built, so that "target" folder is in the project directory filled with classes. If it is not, I get a NoClassDefFoundError.
The error looks like this:
Exception in thread "main" java.lang.NoClassDefFoundError: SOME/CLASS/THAT/IS/EXPECTED/IN/TARGET/FOLDER/OF/SOME/MODULE/THAT/IS/A/DEPENDENCY
at ...
Caused by: java.lang.ClassNotFoundException: com.example.SOME.CLASS.THAT.IS.EXPECTED.IN.TARGET.FOLDER.OF.SOME.MODULE.THAT.IS.A.DEPENDENCY
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
My POM looks like this:
<parent>
<groupId>some</groupId>
<artifactId>parent</artifactId>
<version>0.9-SNAPSHOT</version>
</parent>
<scm>
<developerConnection>scm:svn:https://someconnection</developerConnection>
</scm>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<configuration>
<tagBase>sometagbase/tags</tagBase>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>myModuleGroupId</groupId>
<artifactId>somModule1</artifactId>
<version>1.2.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
...... many of these here
</dependencies>
Why does it suddenly require a target folder with classes? Is it not supposed to get them from the dependencies? From the repository?
Open maven tab at right side of the editor and run double click all options of life cycle and try to see if folder finally generated
I'm trying to start up with http://www.sparkjava.com/, a small Java web framework. The instructions tell you to add it as a Maven dependency (done), but when I mvn package, I get a class def not found for spark/Route.
I assume this is from Spark not being in my classpath. How can I add it? Would it go in pom.xml?
EDIT: Sorry, here is my pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bernsteinbear.myapp</groupId>
<artifactId>myapp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>myapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
EDIT: Trace
λ chaos myapp → java -cp target/myapp-1.0-SNAPSHOT.jar com.bernsteinbear.myapp.App
Exception in thread "main" java.lang.NoClassDefFoundError: spark/Route
Caused by: java.lang.ClassNotFoundException: spark.Route
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
aaaand the source (the example from the homepage):
λ chaos myapp → cat src/main/java/com/bernsteinbear/myapp/App.java
/**
* Hello world!
*
*/
package com.bernsteinbear.myapp;
import spark.*;
import static spark.Spark.*;
public class App {
public static void main(String[] args) {
get(new Route("/hello") {
#Override
public Object handle(Request request, Response response) {
return "Hello World!";
}
});
}
}
What works for me to make it run:
mvn package
mvn exec:java -Dexec.mainClass="com.your.class.with.main.method"
I was facing the same problem when trying to deploy the application to Heroku. I added the following to my POM.xml. This plugin ensures that the maven dependencies are copied over to your application.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals><goal>copy-dependencies</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
After this you can go ahead and run
java -cp target/classes:"target/dependency/*" com.bernsteinbear.myapp.App
to run your application
Ok, so the maven package itself did not throw the exception; it was execution. The package produced by Maven must not contain everything required to run the app. (You can unzip the jar if you're curious about exactly what it contains.) So now it's a matter of either including Maven dependencies to your packaged classpath (I wouldn't necessarily recommend bothering with that yet), or instead simply including additional JARs in your runtime classpath (for Unix looks like -cp a.jar:b.jar:...). I suspect the spark-dependencies module has all the missing dependencies. (Unfortunately the readme is not very clear on this.)
Assuming the spark-dependencies module is sufficient, you'd just do:
java -cp target/myapp-1.0-SNAPSHOT.jar:lib/jetty-webapp-7.3.0.v20110203.jar:lib/log4j-1.2.14.jar:lib/slf4j-api-1.6.1.jar:lib/servlet-api-3.0.pre4.jar:lib/slf4j-log4j12-1.6.1.jar com.bernsteinbear.myapp.App
Note you have to get the paths right. This is assuming the spark-dependencies zip file is unzipped to a lib folder.
If that still doesn't do it, or for additional information or to give feedback, you might also ping the author directly.