I have a java application where main class is dependent with some other classes. I can run the application using an IDE well. But when I want to run the application from command line by locating to the main class's class file using java mainClass, it gives me the following error [the main class name is mainClass]:
Exception in thread "main" java.lang.NoClassDefFoundError: mainClass(wrong name: mainfolder/mainClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Can anybody suggest me how can I run my application without using any IDE?
Uou need to indicate the fully qualified name. For example if MainClass is under package com.example, you should execute java com.example.MainClass
You can check more here.
From documentation:
By default, the first non-option argument is the name of the class to
be invoked. A fully-qualified class name should be used. If the -jar
option is specified, the first non-option argume nt is the name of a
JAR archive containing class and resource f iles for the application,
with the startup class indicated by the Main-Class manifest header.
The Java runtime searches for the startup class, and other classes
used, in three sets of locations: the bootstrap class path, the
installed extensions, and the user class path.
Try mainfolder.mainclass instead of mainfolder/Mainclass. Also, the mainfolder is the package.
Make sure you put all needed jars in the classpath with -cp.
With or without IDEs, java application can always start by
java -cp classpath EntryClass
If you run the application with IDEs, the IDE you using prepares the classpath for you. If you run without the IDEs, you should prepare the whole classpath yourself.
The EntryClass is also found through the classpath.
Please refer to java - the Java application launcher for details.
Let say you are runngin your java program from /home/flyleaf/app/
This would be the command you need:
javac -classpath /home/flyleaf/app/jar1.jar:/home/flyleaf/app/jar1.jar:/home/flyleaf/app/ com/app/your/mail/class/Main.java
You need to add to the classpath the jar files you need and the path where you got your program
Ok! Got the output after executing the java command over the main class's class file with package.classname from outside the folder that contains the class files!
Besides the jar file is also working after locating the jar file and by executing this command java -jar jarname.jar!
Thanks everybody!
Related
I run the following command:
C:\Projects\java -cp ./dependency.jar -jar ./dist/main.jar
Exception in thread "main" java.lang.NoClassDefFoundError: MyException
Caused by: java.lang.ClassNotFoundException: MyException
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: Main. Program will exit.
MyException is inside dependency.jar. I also tried to replace -cp with -classpath. What do I do wrong?
The -cp options is ignored if you use -jar. If you need extra jar-files on the class-path, you should specify this in the Manifest of main.jar like this:
Class-Path: dependency.jar
(Then you can run your program using java -jar main.jar.)
Related questions:
When creating a jar file how do I retain the existing classpaths to other jar files?
Cannot find class even when jar file is in working directory
Jetty Classpath issues
problem related to MANIFEST.MF in jar
Have you tried specifying the class path in the windows environment variable CLASSPATH ?
specify your environment variable CLASSPATH and put the value of your JAVA_HOME\lib and (path to all your classes) and also include current directory by including (dot or period sign).
I have three source files in a folder. I simply want to compile them using commandline and then execute them. However, I'm having the following issue. I am on a windows box:
Code compiles fine:
C:\mycode\src\code>javac Source1.java Source2.java Source3.java
Does not run from the folder where class files are:
C:\mycode\src\deckofcards>java Source1
Exception in thread "main" java.lang.NoClassDefFoundError: Source1 (wrong name: code/Source1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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: Source1. Program will exit.
However, it does run when moving to parent folder:
C:\mycode\src\code>cd ..
C:\mycode\src>java code/Source1
Hello1
Hello2
Hello3...
......
....
I want to distribute this source to someone and I'm not sure whether they will be on windows or unix box.
Eventually I want to create an executable jar file but that also was giving me an error like this.
Problem here is not with running Java files, it runs fine. So you wanted to understand the following:
In order to run a Java program you need to do the following steps:
Create Java source files (*.java files)
Compile using javac to binary class files (*.class files)
Run the class files using java executable
We use package structure to keep organize the java files. In your case it is a simple application (and you use the package named "code"), but in a large production application we need to organize our code into many packages.
While running the class file, we need to give the class file path (which includes the package path). So in your case you need to execute java code/Source1 to run your class.
You can learn more from this link: http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html
For creating a JAR file you can refer the link: http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
For making a JAR file executable ensure you add manifest file with main-class attribute.
I'm trying to start my java game but I have some troubles with the java command line:
Here is what I type:
C:\>java -Djava.library.path=%cd%\lib -jar game.jar
And here is what I got:
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/LWJGLException
at com.game.Main.main(Main.java:7)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.LWJGLException
at java.net.URLClassLoader$1.run(Unknown Source)
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)
... 1 more
I can start my game with Eclipse (With Run as Java Application) but after the menu I got the following error:
Exception in thread "Thread-5" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at org.lwjgl.Sys$1.run(Sys.java:72)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:65)
at org.lwjgl.Sys.loadLibrary(Sys.java:81)
at org.lwjgl.Sys.<clinit>(Sys.java:98)
at org.lwjgl.opengl.Display.<clinit>(Display.java:129)
at com.game.displaygui.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
lwjgl.jar has been put into the folder \lib.
Could you explain me with I got that?
Thanks.
This because lwjgl library is made by two components:
the .jar file which contains Java code
and the native binary library (which can be .so or .dll or .dylib according to your OS)
The first error you are getting is because you are setting the library path, that should contain the native library, but it does contain the .jar. So you get a java.lang.NoClassDefFoundError because you should set either the library path to the folder that contains native library, either the classpath to contain the real lwjgl.jar file.
The second error that you get with Eclipse is a successive step: your classpath contains the jar library but it is not able to find the native library attached to it, you can fix it in the following way:
You should specify explicitly which lib files to include (seperated by ;) :
-cp %cd%\lib\lwjdl.jar;%cd%\lib\<another-lib>.jar
Don't be tempted to use the wildcard * as it will cause more harm than good (from previous experience :))
You use the java.library.path option to specify the location of native libraries to load. In the location pointed to by that option you would place all dll or .so files required by LWJGL. On the other hand, you need to make sure that all required JAR files are on your classpath, via the -classpath option. Currently, you have your JAR files in the wrong directory.
In Eclipse, if you using maven.
Add the following to the project "Run" : "VM options" .
-Djava.library.path=yourpath/youproject/java/target/natives
I have compiled a java project into a jar but I am having some issues when trying to run it
java -jar XMltoCSV.jar
Error: Could not find or load main class XMLtoCSV.class
I have created a manifest file that contains:
Main-Class: xmltocsv.XMLtoCSV
(there is a newline character in the manifest text file)
The XMLtoCSV.class does contain the main() method. I have been running this code to create the jar file:
jar cvf XMLtoCSV.jar Manifest.txt xmltocsv/*.class
And I know its working because I get:
jar cvfm XMLtoCSV.jar manifest.txt XMLtoCSV.class
added manifest
adding: XMLtoCSV.class(in = 4885) (out= 2492)(deflated 48%)
Why does it give me this error? Thanks in advance for the help
It seems I am also running into another error, what does it mean?:
Exception in thread "main" java.lang.UnsupportedClassVersionError: xmltocsv/XMLt
oCSV : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
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: xmltocsv.XMLtoCSV. Program will exit.
Main-Class: XMLtoCSV.class
it's not right.
You have to specify something like:
Main-Class: com.mydomain.XMLtoCSV
namely the fully qualified name of the class.
See: http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
Drop the .class in your manifest.
Update:
What is the package of your main class? If your Java-File contains a statement such as
package com.google.common.base;
at the top. You need that package in your manifest. In your case that would be:
Main-Class: com.google.common.base.XMLtoCSV
If your class does not have such a package statemnent the error is something else.
(Note that com.google.common.baseis just an example of course.)
For a jar be executable, you have to be concerned with two thing on the manifest file:
1- The 'Main-Class' entry must be pointing to the class that'll be launching the application, i.e. the class where you declared the main method to start your app.
2- After all entries on the manifest file, you have to leave three lines in blank (I never found a documentation explaining why that is required but always works for me).
The simplest MANIFEST file would look like the following:
Manifest-Version: 1.0
Main-Class: com.foo.FOO
Just remember to leave three lines in blank after the last entry.
Hope I have helped.
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.