java.lang.NoClassDefFoundError at Runtime: Need Class not on Classpath - java

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.

Related

Java cannot find a class in a .jar file in Linux

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

Java SQLite org.sqlite.JDBC classpath broken?

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.

when running the java program, report cannot find class

The file FactoryDemo.java is compiled successfully and in it there is the
package home.city.Desktop.factorydemo;
I compile it by using javac -d / FactoryDemo.java and nothing wrong is reported.
But when I try to run it by using java home.city.Desktop.factorydemo.FactoryDemo, it reports:
Exception in thread "main" java.lang.NoClassDefFoundError: home/city/Desktop/factorydemo/FactoryDemo
Caused by: java.lang.ClassNotFoundException: home.city.Desktop.factorydemo.FactoryDemo
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: home.city.Desktop.factorydemo.FactoryDemo. Program will exit.
I have tried many times but I could not find the solution to the problem. Thanks for your help in advance.
Your compiled class must be in a directory called:
home/city/Desktop/factorydemo
On Unix, just run this:
mkdir -p home/city/Desktop/factorydemo; mv FactoryDemo.class home/city/Desktop/factorydemo/
For best practice, simply move your Java source file in that folder too so the compiler outputs the file to the right spot.
FactoryDemo Class is created under root directory, you should type following command ( . instead of /)
javac -d . FactoryDemo.java
It creates home.city.Desktop.factorydemo directories starting from current directory.
If you want class created under root directory. You must type following command to execute.
java -classpath / home.city.Desktop.factorydemo.FactoryDemo

Runnig jar dependant java class file

java.lang.NoClassDefFoundError
I have a java program which is dependant on two jar files.
i compile the program using command :
javac -classpath jar1.jar:jar2.jar myprog.java and it compiles successfully.
But when i try to run the program using command : java -cp jar1.jar:jar2.jar myprog , it is throwing the java.lang.NoClassDefFoundError . please help , where am i wrong ?
i am using ubuntu 10.04.
Actual error :
Exception in thread "main" java.lang.NoClassDefFoundError: userapps/SelectionTask_classes/SelectionTask
Caused by: java.lang.ClassNotFoundException: userapps.SelectionTask_classes.SelectionTask
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: userapps/SelectionTask_classes/SelectionTask. Program will exit.
where SelectionTask is my class file generated after successful compilation.
Make sure that the current directory is also in the classpath. Try running with:
java -cp .:jar1.jar:jar2.jar myprog
if your compiled class file myprog.class is in the current directory.
(This assumes that your program is not in a package).
You need to add directory where your compiled java program (*.class) file is located. If it's in current directory then you could run it like this (notice "." which denotes current working directory):
java -cp .:jar1.jar:jar2.jar myprog
Look at the error, it's complaining about not being able to find "userapps.SelectionTask_classes.SelectionTask", which hints at the last entry in your classpath not beign specified correctly. Now, assuming that the JAR files you have specified are in the following directory structure:
./hadoop-0.20.1-core.jar
./lib/hadoopdb.jar
./userapps/SelectionTask_classes/
You would run:
java -cp hadoop-0.20.1-core.jar:lib/hadoopdb.jar:userapps/SelectionTask_classes/ myprog

Jythonc : How to use?

I am new to jython. Here is my failed attempt to use javac to complie my python script into class file.
rohit#rohit-laptop:~/backyard$ echo "print 'hi'" > test.py
rohit#rohit-laptop:~/backyard$ jython test.py
hi
rohit#rohit-laptop:~/backyard$ jythonc test.py
Warning: jythonc is unmaintained and will not be included in Jython-2.3. See http://jython.org/Project/jythonc.html for alternatives to jythonc. Add '-i' to your invocation of jythonc to turn off this warning
processing test
Required packages:
Creating adapters:
Creating .java files:
test module
Compiling .java to .class...
Compiling with args: ['/usr/bin/javac', '-classpath', '/usr/share/java/jython.jar:/usr/share/java/servlet-api-2.4.jar:/usr/share/java/libreadline-java.jar:./jpywork::/usr/share/jython/Tools/jythonc:/usr/share/jython/Lib:/usr/lib/site-python:__classpath__', './jpywork/test.java']
0
rohit#rohit-laptop:~/backyard$
rohit#rohit-laptop:~/backyard$ cd jpywork/
rohit#rohit-laptop:~/backyard/jpywork$ ls
test.class test.java test$_PyInner.class
rohit#rohit-laptop:~/backyard/jpywork$ java test
Exception in thread "main" java.lang.NoClassDefFoundError: org/python/core/PyObject
Caused by: java.lang.ClassNotFoundException: org.python.core.PyObject
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: test. Program will exit.
rohit#rohit-laptop:~/backyard/jpywork$
Please suggest the correct way.
Jython will automatically compile it into .class file on first execution, however in case you want to compile it, you can use standard library, find py_compile.py or compileall.py on Jython lib folder.
$ java test # is not the exact command to execute the class file
this class file requires jython.jar file to execute you have to give the jython.jar path at the run time.
#java -cp /home/litindia/jython-2.0.1/jython.jar:. test
-cp is compulsory /home/litindia/jython-2.0.1/jython.jar is my path where is jython.jar is present in my system, your path may be different. so please give the appropriate path.
And after the path :. is compulsory. Then file name is required.

Categories

Resources