My Program.java is a class declared like so:
public class Program extends JFrame {
...
The program compiles and runs just fine, but when I'm creating a jar file I am recieing this error:
Exception in thread "main" java.lang.NoClassDefFoundError: Program/jar
Caused by: java.lang.ClassNotFoundException: Program.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)
Here is how I am creating the jar file (Linux):
javac Program.java
echo Main-Class: Program >manifest.txt
jar cvfm Program.jar manifest.txt Program.class
Is this because I'm not including JFrame.class in with my jar creation? If so, is the source for JFrame available so I can include it?
Thank you and have a good one!
You need to look for the following things:
Your jar file contains a Manifest file on META-INF
That manifest file includes the main class.
Your .class files are on the appropriate folders.
Your main class has a public static void main(String[] args) method.
Then you should be able to run the program by calling
java -jar yourjar.jar
Cheers.
Related
I am trying to run a code with multiple Java class files and a jar file which is from a library I downloaded. I compiled them with the following:
javac -cp "quickfixj-all-.jar" BTCCMarketDataRequest.java Bot.java
The Bot class has the main method and the BTCCMarketDataRequest file has a bunch of other methods in the class. I am not creating any packages.
How should I run it though?
If I do:
java Bot
I get the following output:
Exception in thread "main" java.lang.NoClassDefFoundError: quickfix/Group
at Bot.main(Bot.java:4)
Caused by: java.lang.ClassNotFoundException: quickfix.Group
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:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
... 1 more
The compiled class (the one in the jar file could not be found, but why? I compiled it.
I am new to Java so I have no idea what's going on.
Thank you!
You need to add the classpath while executing the program as well.
java -cp .:quickfixj-all-.jar Bot
This assumes that the Bot class is in the default package and all the jar and .class dependencies are in the same directory.
I built a java program in Eclipse in Windows and it worked well. The program included 5 java classes and 5 jar library files.
Then I copied all *.java files and *.jar files to Linux. After I compiled and run it, I got an exception, the class PaserException of htmlparser.jar was not found.
Exception in thread "main" java.lang.NoClassDefFoundError: org/htmlparser/util/ParserException
Caused by: java.lang.ClassNotFoundException: org.htmlparser.util.ParserException
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:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
I just begin to learn Linux so I'm not sure if I compiled and run it correctly.
All of my files(*.java and *.jar) are in the same directory, and my operation is in this directory.
To compile:
javac -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar *.java
To run:(the main method is in Crawler class, main method requires at lease 1 argument)
java -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar Crawler arg0 arg1
Then I got the exception above. Did I compile and run it correctly? Why did I got this exception? Thanks.
The folder that holds the class file (.) is not on the classpath. Add another :. to the classpath:
java -cp ./htmlparser.jar:./filterbuilder.jar:./sitecapturer.jar:./thumbelina.jar:./htmllexer.jar:. Crawler arg0 arg1
Lets say i have a class file A.class and i keep it inside some package of an already exisiting web project in deployed in glassfish server in linux. A.class has many dependent with many other classes in same package or other package of the same web project.
Now i cd into the folder and try to run java A it throws following exception Exception in thread "main" java.lang.NoClassDefFoundError /path/A.Is it because the class file coulnot find its dependencies?. Is there any way to make it run.
Error log. Here the class file i want to run is ChangeBulkPassword
Exception in thread "main" java.lang.NoClassDefFoundError: com/project_name/ChangeBulkPassword
Caused by: java.lang.ClassNotFoundException: com.project_name.ChangeBulkPassword
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)
Could not find the main class: com.project_name.ChangeBulkPassword. Program will exit.
Edit:
I cd to project_name\WEB-INF\classes and then run java packagename.ChangeBulkPassword.It throws exception that i cannot find required library.Is there a way to reference those libraries?
If your A.class has a package name com.xxx.yyy there must be a directory tree com/xxx/yyy,you should cd to the directory where com directory exists and then run the class by type java com.xxx.yyy.A.
EDIT:
If there is a directory tree:
/home/me/webapps/com/project_name/ChangeBulkPassword
cd
cd webapps
java -classpath "." com.project_name.ChangeBulkPassword
I am trying to run a test.class file from the linux command line. I compiled the file using javac test.java which produced the test.class file. When I run the command java test it throws a class not found exception. I also tried specifying the package name with the same result. Here is the output, can anybody help? I believe my syntax is correct according to Google searches.
[root#localhost usr]# java test
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: testJava/test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
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)
Could not find the main class: test. Program will exit.
You need to compile your .java file with javac:
javac MyFile.java
Then run the file using java
java MyFile
If you're doing this and it still doesn't work, that means you need to make sure the file and the class name inside the file have the same name... so MyFile.java contains
class MyFile
{
// ...
public static void main(String[] args)
{
// ...
}
// ...
}
Use
java testJava.test from the parent directory of testJava
Assuming your class looks like this:
package javaTest;
public class test{
public static void main(String[] a){
System.out.println("Test");
}
}
Then to compile and run the file, you basically do this:
$ ls
testJava
$ ls testJava
test.java
$ javac testJava/test.java
$ java testJava.test
Test
What this means is that you have to run the class with its fully qualified name, which includes the package name of the class.You also have to do it from the directory that conatins the root of your package. (Unless you specify the "root" directory with the -cp flag.)
I too had problem initially.
java -cp . Test
import java.io.*;
public class ArrayApp{
public static void main(String[] args){
System.out.println("lllll");
} // end main()
} // end class ArrayApp
i get this error when i run my application after compiling it.
Exception in thread "main" java.lang.NoClassDefFoundError: ArrayApp
Caused by: java.lang.ClassNotFoundException: ArrayApp
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: ArrayApp. Program will exit.
You need to make sure your class file is in the classpath. Assuming you are using the default package (i.e. no package declaration), you need to tell Java where to find your class when you run it. So let's assume your ArrayApp.class file is in the same directory. You will need to run it like this:
java -cp . ArrayApp
The option -cp and the following . tell Java that the classes will be in the current directory. The longer name for -cp is -classpath, so you can use that as well.
Also note the space between the classpath and the class name. The path is the base directory of where your class files are located. If you compiled them into a directory named "bin" then you would change the way you call Java like this:
java -cp bin/ ArrayApp
The "ArrayApp" is the fully qualified class name.
Your classpath is incorrect. Try...java -classpath . ArrayApp