So when exporting the project as a runnable jar, the jar works fine on the current machine.
Moving the jar to another machine, and it cannot find the main class:
Used cmd to get the error:
Desktop>java -jar RunMe.jar
Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 500
at pnl_user.readFile(pnl_user.java:667)
at pnl_user.readTNS(pnl_user.java:432)
at pnl_user.<init>(pnl_user.java:412)
at main.<clinit>(main.java:9)
... 3 more
To run a JAR file use java -jar thejarfile.jar.
java runme would try to run the class runme from the current classpath (which is probably not set up to point to your application).
Is this runme the jar name or main class in the jar?
To launch your app from a jar, you can do the below:
java -jar runme.jar
Related
I have a JAVA (awt) Project to build a desktop base utility. When I try to run this from other machine with only JRE Installed this is giving an exception as
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at DataProcessor.DataProcessor.App.<clinit>(App.java:73)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
at java.base/java.net.URLClassLoader.findClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
here I am not using a maven project, creating an executable JAR directly from eclipse. All though I see the JAR for log4J (log4j-1.2.17.jar) in the MANIFEST. But this is giving an exception when I try to run it from other machines. ( Other machines has only JRE not JDK) following command I am using to run JAR
C:\Users\<username>\Desktop\Utility\latest>"C:\Program Files\Java\jre1.8.0_161\bin\java.exe" -jar Utility.jar
The exception is sayin' that you don't have the log4j dependency found on your class path: are you providing the log4j.jar in the same folder as the one in which your jar is placed?
It is not so easy to reference other jars when running an application as java -jar yourApp.jar ...
You can take a look to this post about this referencing a jar when running another one;
If you want to investigate about MANIFEST usage, take a look here;
However, i suggest you to use maven (or do it manually) exporting a fatjar with the needed dependencies packaged into (take a look to maven-assembler plugin or Eclipse runnable jar export options).
I have a simple jsoup test app in a single folder, in which there are 2 classes - LyricsGetter.java and Main.java - and a .jar file with jsoup library. When I compile the files with command javac -cp jsoup-1.8.2.jar LyricsGetter.java Main.java everything compiles ok, but when I try to run with java Main, I get:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
at LyricsGetter.getLyrics(LyricsGetter.java:16)
at Main.main(Main.java:14)
Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
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
On the other hand if I run with java -cp jsoup-1.8.2.jar Main, then I get Error: Could not find or load main class Main. So, what is the correct way to run this program?
You need to tell Java to look for classes in jsoup-1.8.2.jar, and the current folder (.).
On Windows, use:
java -cp jsoup-1.8.2.jar;. Main
or on Linux, OSX, or other Unix-like systems use:
java -cp jsoup-1.8.2.jar:. Main
(The difference is that the paths are separated by ; on Windows or : on *nix)
I made a java class using the library Jaudiotagger, and it runs without problems. I then made a jar out of it and I got NoClassDefFoundError. Here's how it went:
I put the main class id3tag.java and the library jaudiotagger-2.0.3.jar in a folder and compiled using the command line. The program ran smoothly without problems.
javac -cp .;jaudiotagger-2.0.3.jar id3tag.java
java -cp .;jaudiotagger-2.0.3.jar id3tag
I then created the manifest and the jar file.
echo Main-Class: id3tag >manifest.txt
jar cvfm id3tag.jar manifest.txt id3tag.class jaudiotagger-2.0.3.jar
I got the following output:
added manifest
adding: id3tag.class(in = 5952) (out= 2997)(deflated 49%)
adding: jaudiotagger-2.0.3.jar(in = 811441) (out= 740599)(deflated 8%)
I then ran the jar file, and got "A Java Exception has occurred.". I also tried:
java -jar id3tag.jar
And I got the output:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaudiotagger/tag/
FieldDataInvalidException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2451)
at java.lang.Class.getMethod0(Class.java:2694)
at java.lang.Class.getMethod(Class.java:1622)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.jaudiotagger.tag.FieldDataInval
idException
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)
... 6 more
I then replaced all (both) error classes used from Jaudiotagger with Exception and recreated the jar. Now the same thing happens as for seemingly all other jar files when I run them (with Java(TM) Platform SE binary, or through writing id3tag.jar in the command prompt) - nothing. java -jar, however, works and gave me a runtime error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/jaudiotagger/audi
o/AudioFileIO
at id3tag.tagSong(id3tag.java:112)
at id3tag.tagAlbum(id3tag.java:82)
at id3tag.tagArtist(id3tag.java:40)
at id3tag.main(id3tag.java:170)
Caused by: java.lang.ClassNotFoundException: org.jaudiotagger.audio.AudioFileIO
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)
... 4 more
It seems like the entire library just doesn't do shit for me. How do I fix this, and how do I get jar files to run without java -jar? (Also, how do I get the full pile'o'errors in case someone needs it, rather than just having it say "x more"?)
I'm not very savvy with this kinda shit so the more specific the answer, the better. Thanks.
I'm on windows 8 and latest java (1.7.0_17).
this is informing you that you have no main class, as you might or might not already know all programs in almost all non web based programming languages must have a main class to start from. In java it looks like this
public static int main(String[] args){
}
The message is informing you that this class is no longer there, this could be a compiler error or something entirely different.
please try downloading the library again, and downloading eclipse ide for java ee developers.
this ide has a built in compiler that will work better than a command line sometimes.
i hope this helped.
I worked around it by including the org folder from jaudiotagger in my .jar instead of the actual .jar file, and I then used a .bat file to run it instead of running the .jar directly. Feel free to add your own answer if you find a better solution, and I'll check back.
I extracted jaudiotagger-2.0.3.jar with winrar so that the org folder was in the same folder as my main class. Then I could compile and run the main class simply by:
javac id3tag.java
java id3tag
I then created the manifest file the same way as before, and created the jar:
jar cvfm id3tag.jar manifest.txt id3tag.class org
The jar file worked with java -jar id3tag.jar, but simply writing id3tag.jar still did nothing. Turns out it's because jar files are by default run by the javaw.exe file, so you have to right click the jar file -> open with -> choose default program and navigate to the java.exe file in your java folder (just search the folder). Running the .jar then gets you "Error: Could not find or load main class path\id3tag.jar". I worked around this by using a .bat file. I entered
java -jar path\id3tag.jar
into Notepad and saved as whatevername.bat (save as type: all files).
I created a new question for getting the .jar file to work properly. See .jar error - could not find or load main class.
I created a executable jar of my Java project I tested the jar and it is working as expected in windows but when i try run the same jar in Unix..
java jar my.jar
..it is throwing me the error below:
Exception in thread "main" java.lang.NoClassDefFoundError: jar
Caused by: java.lang.ClassNotFoundException: jar
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
How to overcome the above error?
It should be
java -jar my.jar
not the
java jar my.jar
See
java - the Java application launcher
In a runnable jar file , there will be a MANIFEST.MF inside the META-INF folder. Inside MANIFEST.MF there should be an entry Main-Class: com.myapp.MyMainClass . This will be created automatically if you using the jar command to make a runnable jar file.To be sure,unjar the jar and see if this entry is present.
I have a java file which uses jfreechart libraries, uses a text file from local drive and displays graph. Runs fine with eclipse. However, I want to run this from cmd prompt, other simple Java files are able to run successfully via cmd prmnt but not able to run this file.
PS: MyTool.java is able to compile without errors and class file is created, but not able to run.
1) This is how I am compiling it in cmd prompt: (gives 0 errors)
C:\Documents and Settings\hello.maga\workspace\MyTool\lib>javac -cp "gnujaxp.
jar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-e
xperimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar" MyTool.java
2) This is how I am running it:
C:\Documents and Settings\hello.maga\workspace\MyTool\lib>java -cp "gnujaxp.j
ar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-ex
perimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar" MyTool
Error for second command:
Exception in thread "main" java.lang.NoClassDefFoundError: MyTool
Caused by: java.lang.ClassNotFoundException: MyTool
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: MyTool. Program will exit.
What I don't understand is, if there are any errors, then it should not compile in first place, can someone educate me. Thank you very much.
You need to include "." in the classpath, like so:
java -cp ".;gnujaxp.jar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-experimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar"
From "Setting the class path": "The class path tells SDK tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java extensions or part of the Java platform. The class path needs to find any classes you've compiled with the javac compiler -- its default is the current directory to conveniently enable those classes to be found."
However if you set the classpath yourself, the default no longer applies, and you're expecting it to load classes from the current directory. You'll have to add it manually, like by adding "." to the class path as Ed Staub recommended.
When compiling, your class wasn't needed on the class path, so to speak, since it's what was being compiled. You only needed all the other classes (in the jar files) on the class path for that. That's why you're able to compile but not run, even though you used an identical class path for both operations.