I create a new jar file. when i run the jar using
java -jar UpdateAvailable.jar com.dao.mysql.UpdateAvailable
I got the following exception.
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jettison/json/JSONException
Caused by: java.lang.ClassNotFoundException: org.codehaus.jettison.json.JSONException
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)
But I copied all the supporting jars inside the UpdateAvailable.jar. Now what my requirement is run the jar file with out specifying supporting jars, like
java -jar UpdateAvailable.jar com.dao.mysql.UpdateAvailable
Here are some points to remember while you are working with JAR files and ClassPath: -
For making your JVM find class, your classpath should be set till the directory containing the class. In this case you have JAR.
So, you need to give the path till your Jar File Name
Now, since you have your Jar files inside your currently running Jar file, you can't set your classpath till the folder inside a Jar, so basically you won't be able to set classpath for inner Jar Files..
So, clearly JVM won't be able to see classes inside your inner Jar..
So, make sure that your required JAR files are outside of all the JAR files (I mean to say - avoid Nested JAR Files)..
I'm not sure how you are packaging your jar file, but it would sound like you need something like One-Jar to help you package your file and all your supporting libraries into a single jar. One-Jar comes with it's own classloader that allows it to load all the included jars in your executable jar file.
Configuration is fairly straight forward and easy to use. If you are using Maven, you can use the one-jar maven plugin to help you configure which libs you want included.
There are also other similar packages you can use instead of One-Jar, such as JarJar and ProGuard. Also take a quick look at this SO question.
In executable jar couldn't reads any supporting jar files inside a jar. So keep all supporting jars in a folder and place the executable jar in the same directory. And add "Class-Path" as "Supporting jar files separate with space" in "MANIFEST.MF" File. Now run the jar as
java -jar UpdateAvailable.jar com.dao.mysql.UpdateAvailable
Related
I'v got a simple java program. I'm developing in Eclipse, using gradle, and I'm logging with log4j2. This all works fine.
When I come to run from the command line, I do a gradlew build which works, but when I run the jar I get an error:
java -jar build\libs\testproj.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at com.xxxxxx.practice.App.<clinit>(App.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
I can see a bunch of log4j files in my userprofile.gradle folder, and I could run a gradle dist task and then got and unzip the resulting zip file and then point at the contents of that, but if there some way of avoiding this? Surely there's something I can add to the manifest or something so that I can run from the command line at will with no overhead?
To be clear, it's just the logging that has this problem. Other than that it runs, the tests work etc from the command line, and the logging works fine from eclipse.
Someplace in your eclipse project, you'll find a library (these end in .jar) named log4j.jar or org.apache.logging-log4j.jar or log4j-api.jar or something along those lines.
This jar contains the classes that are missing when you run this on the command line. Try it: jar tvf log4j.jar prints the contents of the jar and you'll find it contains, for example, org/apache/logging/log4j/LogManager.class.
During compilation and during running a java app, this class needs to be on the classpath. Eclipse is taking care of the compilation part of it.
If you run java as java com.foo.ClassName, the classpath is defined by the -cp parameter: java -cp testproj.jar:log4j.jar com.foo.yourapp.TestApp would work (use semicolon on windows instead). If you don't specify it, the system environment variable CLASSPATH is used, but you don't want that (you can run multiple java apps on one machine after all, so a global setting makes little sense).
If you run java using java -jar myjar.jar however, the classpath is solely taken from the jar file itself; inside the jar file is a file called META-INF/MANIFEST.MF and it contains (in text) key/value pairs. The relevant one is the Class-Path: foo.jar bar.jar entry: Space-separated listings of jar files, relative to the dir your jar is in.
You cannot use the -cp option when using the -jar option; the -cp option is ignored.
So, next steps:
I don't know if gradle made the Class-Path entry correctly. Best option is to use the jar or zip tool to unpack your jar to check that MANIFEST.MF file and see what's there. If there is, say, Class-Path: lib/log4j.jar in that file, then make sure that if testapp.jar is at /Users/Dev123456/project/testapp.jar, that that log4j jar is at /Users/Dev123456/project/lib/log4j.jar. If there is no class-path entry, then the error lies in your gradle config; you'd have to post your build.gradle file in that case.
There are 'striper plugins' which mix all the various jars together in one giant jar. This is usually a bad idea (it just makes the jar itself humongous, and makes builds take longer. If you're deploying java code to a server, you can manage the Class-Path entry by yourself, and if you are making a desktop app, you need an installer which... can manage the class-path stuff just as well. Really no reason to use stripers). I advise against using these.
Just run with java -cp yourapp.jar:dep1.jar:dep2.jar com.foo.fullyqualified.ClassName instead.
Try to run your jar by specifying Log4j jar as well in classpath and then mention your MainClass from testproj.jar :
java -cp build\libs\testproj.jar:build\libs\log4j.jar com.package.MainClass
I've created a method in a Java class that does some encryption. This class utilizes methods from some external jar files. The requestor of this method has asked that I include my class and the external jar files in one jar file. I am using Eclipse for my IDE. As I understand it, I need to create an executable jar file in Eclipse, in order to package the external jar files in my own jar file. In my project, I also created a basic class with a main method in my project that simply calls my method mentioned above (to provide a "main class" for the run configuration for the executable jar). I exported my project to an executable jar file, and verified that it runs (java -jar jar-file-name) in a command prompt window. I created a second test project in Eclipse, and added my executable jar file to the build path. I created a simple class in this test project with a main method that calls the method mentioned above (on the class in my executable jar file). This compiled, so I exported this project to a normal jar file, and tried to run it (with java -classpath executable-jar-file-mentioned-above;test-project-jar-file class-name. This fails with a NoClassDefFoundError, referencing a class found in one of the external jar files.
What am I missing here? This is my first experience with an executable jar file. Is there anything special that needs to be done when calling methods on a class in an executable jar file?
There is nothing special about executable jar files. The only difference is that an executable jar file defines a main class (with a main() method) that will be started when you run java -jar ....
A jar file is in fact a plain zip file containing classes, resources and some additional meta-information.
When you run your jar file and get a ClassNotFoundException, this is an issue with you class path. So you are somehow missing classes, you depend on, located in your external lib.
There are two ways to fix your issue:
Add the library jar to the class path when you run your runnable jar
Repackage your executable jar to contain all the classes from the external library
I don't know what the Eclipse export functions do. But I would recommend taking a look at the exported jar file first using your favorite zip utility.
I would also recommend using a build tool like maven to create your jar. Here is an example how this can be done using the maven assembly plugin: https://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/
I have a small project which in my local IDE is running just fine, a screenshot will show the project structure:
I have highlighted the jewelcli jar because on that class i get the exception mentioned in the header.
While running locally all goes fine, on a testing ubuntu server i get:
java -verbose -classpath "/correct/path/jarName.jar:lib/jars/*" correct.package.and.main.Class
***all bootstrapping class from RT.JAR***
java.lang.NoClassDefFoundError: uk/co/flamingpenguin/jewel/cli/ArgumentValidationException
at correct.package.and.main.Class.main(Class.java:31)
Caused by: java.lang.ClassNotFoundException: uk.co.flamingpenguin.jewel.cli.ArgumentValidationException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 1 more
I dont think this is actually related at all to that jewelcli jar in particular, because i double checked and its there:
unzip -l jarname.jar | grep jewelcli
435937 11-20-11 01:22 lib/jars/jewelcli-0.7.6.jar
But it happens for that Jar because it is the very first jar that the program needs (after all the bootstrapping classes in rt.jar).
This is where i am basing my command line from
But i also see:
Subdirectories are not searched recursively. For example, foo/* looks for JAR files only in foo, not in foo/bar, foo/baz, etc.
I thought that by specifying in the command line the jar file containing all the libraries, java would automaticaly, and recursively, scan all subdirectories, but it does not work:
java -verbose -classpath "/correct/path/jarName" correct.package.and.main.Class
Any tip on how can i get java to scan the whole jar i specify in the -classpath option?
I thought that by specifying in the command line the jar file containing all the libraries, java would automaticaly, and recursively, scan all subdirectories
It depends. The standard Java classloaders do not know how to load classes from JARs that embedded inside other JARs.
And that's what you appear to have done ... if I am reading your question correctly.
There are a few ways to address this:
Copy all of the dependent JARs to the server, put them into a directory, and add the directory to the classpath; e.g.
java -classpath "/correct/path/jarName.jar:/correct/path/lib/*" \
correct.package.and.main.Class
Build an UberJAR or shaded JAR by unpacking the dependent JARs into a directory, adding your classes and producing a JAR from the tree. There are maven plugins for building such JAR files.
Use something like Spring Boot which uses a classloader that understands how to deal with JARs withing JARs.
I'm writing a short utility that performs a short task on a remote Oracle instance. The utility uses the Oracle jdbc driver found in ojdbc6.jar.
The Oracle jar is properly included in my classpath. When I export my project to a jar and run the jar on the command line using "java -jar ", I get:
Exception in thread "main" java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
After moving the Oracle jar into the same directory as my runnable jar file, I changed my Manifest file to include the the Oracle jar in the classpath. This fixed the problem, and my jar ran fine.
Can anyone explain why including the Oracle jar in my classpath didn't solve the problem, and why editing the Manifest file did?
Thanks!
From Sun (Oracle) documentation for the java -jar command:
Execute a program encapsulated in a JAR file. The first argument is
the name of a JAR file instead of a startup class name. In order for
this option to work, the manifest of the JAR file must contain a line
of the form Main-Class: classname. Here, classname identifies the
class having the public static void main(String[] args) method that
serves as your application's starting point. See the Jar tool
reference page and the Jar trail of the Java Tutorial for information
about working with Jar files and Jar-file manifests. When you use this
option, the JAR file is the source of all user classes, and other user
class path settings are ignored.
I am new to Java and I am not sure how apt is my question title. Please suggest any better title.
I have got a Java API which has many executable modules which do various things related to parsing MS Outlook's .pst files. And my problem is that I am getting this exception when I run the execute command to execute a .class file of one module of the API(java -classpath /opt/Java/libs/JPST/lib/jpst.jar /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example)-
Exception in thread "main" java.lang.NoClassDefFoundError: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example
Caused by: java.lang.ClassNotFoundException: .opt.Java.libs.JPST.examples.GetInboxMessages.bin.Example
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: /opt/Java/libs/JPST/examples/GetInboxMessages/bin/Example. Program will exit.
So far ...
You can check my previous question where I concluded that I was correctly running the compile and execute commands. But I am still getting the above mentioned `exception.
I asked the API's developer who says that I should get Eclipse or Netbeans IDE. He said that the
examples folder inside the API's
folder structure is entire Eclipse
workspace.
and also said that
It is easier to compile and run all
examples.
And now ...
So, I have setup Eclipse Helios for Java. Can anyone tell me how is it easy to compile and run all the examples (I think the modules inside the examples folder) using the IDE. I have earlier worked with Eclipse for PHP but never for Java.
Folder structure of the API
I need to execute a .class file of one of those modules (many module folders are present inside examples directory). There are many such modules present inside the examples folder of the API. Each of these modules folders contain a bin folder (containing .class file) and src folder (containing .java file) . I need to execute one such .class file.
There is a lib directory containing a .jar file (which is needed to be referenced by my target .class file). The lib directory is at the same level as the examples directory.
Solution
I have verified that the solution given by Jonathan works (verified only without-using-elipse case). To make it more clear the classpath needs to include all the class locations in both compile and execute commands. So, if you need to compile and execute an Example.java file with a dependency Parent.jar do this:-
Compile
javac -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.java/file/ Example.java
Execute
java -classpath /path/to/Parent.jar/file/Parent.jar:/path/to/Example.class/file Example
And note the separator - should be : for linux and ; for windows.
Thanks,
Sandeepan
When Eclipse starts it should ask you which workspace to use. Select the examples folder and, if it really is an eclipse workspace, you should see all the projects already set up (it may take some time to build everything depending on the size of the projects and the speed of your machine). Then simply find the class you want and run it. If everything is set up correctly it should just work. If not you may have to go back to the developer for more details.
But I don't think you need Eclipse to run that one class. Looking at the stacktrace you provided at the beginning of your question it looks like it failed to run because you haven't set your classpath properly. Try:
java -classpath /opt/Java/libs/JPST/lib/jpst.jar:/opt/Java/libs/JPST/examples/GetInboxMessages/bin Example
To very briefly explain: The classpath lists the places where Java looks for the classes it needs. You specify the class to run relative to the classpath, not as a file path. Since the class you are trying to run in in the folder "/opt/Java/libs/JPST/examples/GetInboxMessages/bin" this folder must be on the classpath (note, I am assuming a unix based system, if you are using windows use ';' to separate entries not ':'). Then simply specify the class to run by itself relative to the classpath (which in this case is just the class name by itself).
Please be aware this is a very basic overview, things get a more complex if you start using packages (folders that would appear below the bin folder). This is one advantage of using a program like Eclipse since it will handle a lot of this for you.
Sounds like a classpath issue.
Right click on your project in eclipse.
Go to buildPath > configure build path
Make sure the jar file is referenced in the libraries tab and if it isn't add it.