I am using Ubuntu 12.04, and I am trying to run a Java program on the command line.
It is a program from a famous programming interview book, Cracking the Coding Interview, and the structure of directories and java files can be seen at
https://github.com/gaylemcd/ctci/tree/master/java
Here, I am running Chapter 2/Question2_5/QuestionB.java, and I use LinkedListNode.java class in CtCILibrary directory, and I also need Chapter 2/Question2_5/PartialSum.java
I went to Chapter 2/Question2_5 directory, and compiled QuestionB.java using the compiling command
javac -cp .:../../CtCiLibrary/CtCILibrary/LinkedListNode.java ./PartialSum.java QuestionB.java
and it compiled(it took a while to figure out the compiling command), and made QuestionB.class file.
I ran java QuestionB, but it throws an error,
Exception in thread "main" java.lang.NoClassDefFoundError: QuestionB (wrong name: Question2_5/QuestionB)
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: QuestionB. Program will exit.
There were so many references when I looked up on Google, but none of them worked for me.
Can anyone tell me how to run this?
You didn't say what directory you were running the java command from. since you did not specify the classpath the command it will look in the current directory (I believe) and down the classpath in the environment. make sure that the file(s) are
available on the classpath
in directory appropriate for their package (if no package name then in the current directory)
Hope this provides some help.
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 stumbled upon a weird error while using JDBC sqlite with org.sqlite.JDBC
my code compiles and runs fine on Windows.
But when I tried moving it to Ubuntu it started showing this:
Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at mall.SQLiteJDBC.<init>(SQLiteJDBC.java:27)
at mall.AllegroReader.<init>(AllegroReader.java:33)
at mall.Mall.main(Mall.java:31)
I'm running it with java -classpath "sqlite-jdbc-3.7.2.jar" -jar Mall.jar" and java -classpath "sqlite-jdbc4-3.8.2-SNAPSHOT.jar" -jar Mall.jar
with both versions in the same directory as my jar and I've tried a dozen different options specifying classpath and it behaves exactly the same. I tried openjdk and oracle jdk.
I tried rebuilding it on Ubuntu, changing ant .xmls, changing paths, etc.
I have no idea what is going on. Pls help.
Here is what happens inside my dist directory:
work1#workwork:/var/www/mall/dist$ ls
mall.db Mall.jar Mall.jar.old sqlite-jdbc-3.8.4.3-SNAPSHOT.jar
work1#workwork:/var/www/mall/dist$ java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall
Error: Could not find or load main class Mall
The classpath is ignored when you use -jar.
You have to either include the dependencies in the jar (or at least have the jar manifest point to them), or run it with -classpath sqlite.jar:Mall.jar the.main.class.
Error: Could not find or load main class Mall.main. all files are there,
my main class comes from Mall.java and is in mall package which
compiles to Mall.jar
So the correct command line is:
java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" mall.Mall
OP findings
to view the classes in jar use jar tf Mall.jar - from this I got mall/Mall.class meaning my class containing main was mall.Mall
it showed
mall/Mall.class
so I should have used mall.Mall as the class to run (instead of pulling my hair)
After spending over 6 hours total with many failed attempts at running "portable" jar package using classpath and whatnot, after having tried OneJar and jarjar to no avail (ended up with Class file too large!) I decided to write the offending piece of code in PHP.
It proved to be more portable than Java in my case.
I have tried several approaches as suggested on this website as well as several others to no avail.
-Running Angstrom embedded Linux, OpenJDK6, Shark VM.
File Structure
All .java and .jar files are stored in directory ./src
jarfiles: j1.jar j2.jar
Source code: Coder.java, WrapperClass.java
Compiling and Running
javac -cp j1.jar:j2.jar Coder.java WrapperClass.java
Successfully compiles, outputs Coder.class, WrapperClass.class, as well as several other files including Coder$1.class through Coder$6.class, as well as Coder$Main_thread.class and Coder$Progress_Bar_Thread.class, any insight onto what all these .class files are for?
Run attempts all terminate in java.lang.ClassNotFoundException.
~/coder/src: java Package.Coder
~/coder/src: java -cp jar1.jar:jar2.jar Package.Coder
~/coder: java ./src/Package.Coder
~/coder: java -cp jar1.jar:jar2.jar ./src/Package.Coder
Thank you very much for your help. I have run this on NetBeans and can verify that it's functional code, just a matter of getting it to run on Linux.
Stacktrace is:
Exception in thread "main" java.lang.NoClassDefFoundError: Package/Coder Caused by: java.lang.ClassNotFoundException: Package.Coder
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:334)
Could not find the main class: Package.Coder. Program will exit
Assuming this structure
/coder
/src
/Package
/Coder.java
/WrapperClass.java
/j1.jar
/j2.jar
You will need to compile from /src as
javac -cp j1.jar:j2.jar Package/Coder.java Package/WrapperClass.java
This will create .class files in /Package. You can then run it, again from /src as
java -cp j1.jar:j2.jar Package.Coder
assuming the Coder class has a main method which is your entry point.
I am trying to run this command on linux... compilation is successfull but while running it gives error.
Executing this command:
java -cp .:/smash/same/hope/ant-launcher-1.6.1.jar src.vp
output
Setting the value for property-debug
Fusion Repository/asd/file/repo
Logs Directory
Running validations
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/Project
at src.vp.call(vp.java:114)
at src.vp.main(vp.java:172)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.Project
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:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
... 2 more
Can some one help me to solve it...
You've included the ant-launcher jar file, but that doesn't contain the whole of ant, I'm sure. Look for the ant jar file that includes the Project class. (I'd expect it to be ant-1.6.1.jar based on the other file you've specified.)
I have been trying for what seems like two days now to get my java application to compile from the command line in Ubuntu. I know I have Java installed because I can run my applications in Eclipse & Netbeans and they work fine. But if I want to compile my applications from the command line I get the following error message:
javac Main.java
Everythings fine, no errors or anything. Then I try:
java Main
And I get this error message:
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: input/Main)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:637)
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:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
Could not find the main class: Main. Program will exit.
Try:
java input.Main
By the looks of your error, your Main class is in package "input". You need to specify package name when running a class, not the filename.
Open terminal and paste this command:
export CLASSPATH=.:/usr/local/tomcat/common/lib/jsp-api.jar:/usr/local/tomcat/common/lib/servlet-api.jar:/home/trenog/javokapi/bin/xmlrpc.jar
This looks like a classic Classpath problem. Eclipse and Netbeans will set up the classpath for you, but when you're writing to the command line, you're on your own.
Assuming you're using BASH, try typing the following into the command line:
CLASSPATH=/path/to/your/java/class/file
Or, alternately, you can do this from the java command line:
java -cp /path/to/your/java/class/file Main
Follow this link for more info.
EDIT: Well, I see you figured it out. Congrats.
The classloader simply can't find the class input.Main.
The class should be located in the directory ./input, the file inside that directory should be called Main.class and the java command should be 'java input.Main'.