java.lang.NoClassDefFoundError when i run java file from terminal - java

I am a java newbie. I have been using Eclipse to test a simple java class (named NewHelloWorld) and it runs fine in the console. When I try to do the same thing from a terminal, it compiles properly (creates a HelloWorld.class without giving any error) , but then java NewHelloWorld shows the following error
Exception in thread "main" java.lang.NoClassDefFoundError: NewHelloWorld (wrong name: org/kodeplay/kodejava/NewHelloWorld)
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: NewHelloWorld. Program will exit.
I also tried java -classpath . NewHelloWorld but that doesnt work as well giving the same error.
These are the values of the environment variables:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
JAVA_HOME="/usr/lib/jvm/java-6-openjdk"
CLASSPATH="/usr/lib/jvm/java-6-openjdk/lib:."
Is anything else required or am I missing anything here?
Thanks
PS: using Ubuntu 10.04 desktop

wrong name: org/kodeplay/kodejava/NewHelloWorld
cd up to the package root, so that you're in the folder containing org folder and then do
java -cp . org.kodeplay.kodejava.NewHelloWorld

The error message gives you a clue:
(wrong name: org/kodeplay/kodejava/NewHelloWorld)
It looks like your class is called org.kodeplay.kodejava.NewHelloWorld. The Java command line needs to know the fully qualified class name:
java -cp . org.kodeplay.kodejava.NewHelloWorld
should do the trick.

Go to the package root directory (the parent directory of org) and run:
java -cp .:$CLASSPATH org.kodeplay.kodejava.NewHelloWorld
Also I wouldn't put . to my CLASSPATH permanently (in .bashrc, .bash_profile or /etc/profile) it may lead to undesired behavior.

cd up to the root package. Most of the cases it will be src folder in the Project if created from eclipse IDE.
java -cp . org.kodeplay.kodejava.NewHelloWorld should work
java org.kodeplay.kodejava.NewHelloWorld should also work. I tried both the things and it works fine in both the case

I had a similar problem running a HelloWorld program I had written with a text editor on Mac OS X. It ran fine on a remote Linux box, but running it from home directory I got the dreaded NoClassDefFoundError.
Found that I could fix it either by running as:
java -cp . HelloWorld
or, without the classpath qualifier, after adding the current directory to my bash CLASSPATH for the current session:
export CLASSPATH=.

FOR MAC USERS : 👇
java -classpath /Users/apple/IdeaProjects/Folder_name/out/production/folder_Name com.Package_name.class_name

Related

How to compile Java programs from Eclipse using terminal on a Mac

I am trying to learn how to compile Java programs made on Eclipse, an IDE, in Terminal. Starting out right after opening terminal, what are the steps I should take to compile a program I've made on Eclipse in terminal? Thanks for the help.
UPDATE: I got as far as navigating to my java folder, and to the package that houses my programs, and did the line javac Hello.java (Hello is the basic "Hello World" program I'm trying to compile) but when I do java Hello I get a large error:
Exception in thread "main" java.lang.NoClassDefFoundError: Hello (wrong name: homeWorkPackage/Hello)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
What did I do wrong?
According to the official tutorials (assuming you have the JDK properly configured:
Change the directory to the directory where your file is saved (using the cd command)
Use the command javac [filename.java] to compile the program
there should be a class file in the directory now
If you are using eclipse, compiling from the terminal is not needed, however you can do it like this:
Navigate to the project directory containing the .java files. You can then run javac ClassName.java e.g. javac Cake.java javac is the Java language compiler. This command will compile the source (your .java file). To run it you can go java ClassName. e.g. java Cake. java starts the JVM. The named class will be loaded and execution started. You don't include the .class file extension one the java ClassName command.
When you need you navigate around the file system I think this page gives a good overview of the commands but here are a few that you might need for this task:
cd - change directory (followed by directory name) e.g. cd Documents
ls - list information about files (can take some parameters)
.. can take you back a directory. e.g. cd .. will bump you back one directory
you can also hit tab to auto complete a directory/file name.

Running java file with external jars in linux

How to run a java file that has external jars in Linux. Also how to run it in Windows?
I tried the following, but not working.
javac -cp c:/lib/lib1.jar;c:/lib/lib2.jar c:/com/example/Application.java
thanks
------EDITED------
Now my class file got generated after compiling without any error. But when i run the file its showing following error
java -cp C:/lib/lib1.jar;C:/lib/lib2.jar C:/com/example/Application
Exception in thread "main" java.lang.NoClassDefFoundError: C:/com/example/Application
Caused by: java.lang.ClassNotFoundException: C:/com/example/Application
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)
Any suggestion...
If you are running on linux, then there should be different directory structure from windows. Also the classpath on linux should contains paths separated path by colon(:) instead of semi-colon(;)
Put your jars in a directory on linux machines, may be in /var/tmp/myjars. And then compile your code using the below command:
javac -cp /var/tmp/myjars/lib1.jar;/var/tmp/myjars/lib2.jar Application.java
EDIT
As you have updated your question. You need to run your class on windows as mentioned here. I am taking few assumptions here that your Application class is in com.example package. If that is the case then you can run it from the folder containing the com folder as mentioned here:
java -cp C:/lib/lib1.jar;C:/lib/lib2.jar com.example.Application
javac is the Java compiler. after compiling the program the running on windows should work like this
java -cp c:/lib/lib1.jar;c:/lib/lib2.jar c:/com/example/Application
You can try this
java -cp classpath=%classpath%;< jar-file full-path>; ClassName
Ex: java -cp classpath=%classpath%;c:/lib/lib1.jar;c:/lib/lib2.jar com.ab.cd.MyClass

java "ClassNotFoundException" error

I am new to java programming and I am getting the much-maligned error "ClassNotFoundException" error.
The strange thing is is that it compiles fine:
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
But when I try to run it, I get the following error:
steve#steve-ThinkPad-T61:~/facebook$ java FacebookProg
Exception in thread "main" java.lang.NoClassDefFoundError:
org/scribe/builder/ServiceBuilder
at FacebookProg.main(FacebookProg.java:8)
Caused by: java.lang.ClassNotFoundException: org.scribe.builder.ServiceBuilder
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
I checked online and it seems that java can't find the library at runtime that it was able to find at compile time. So tried the following variations:
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
java -cp /usr/share/java/ FacebookProg
export CLASSPATH="/usr/share/java"; java FacebookProf
export CLASSPATH="/usr/share/java/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
I checked several places on StackOverflow and google and still can't figure out why. I'm new to java, so there's probably a simple solution, but I can't find it. I am using Sun Java 1.6 64-bit on Ubuntu 11.04. The scribe-1.3.0.jar file is in "/usr/share/java" which, I believe, is the canonical place to put java packages.
The barebones code is here (in case it matters):
import org.scribe.builder.*;
import org.scribe.builder.api.*;
import org.scribe.oauth.*;
public class FacebookProg {
public static void main (String args[]) {
OAuthService service = new ServiceBuilder()
.provider(FacebookApi.class)
.apiKey("blah_blah")
.apiSecret("blah_blah")
.build();
}
}
The classpath has to point to BOTH the directory of the external library you are using AND the class you are trying to run itself. Try this:
Windows:
java -cp .;/usr/share/java/scribe-1.3.0.jar FacebookProg
Linux:
java -cp .:/usr/share/java/scribe-1.3.0.jar FacebookProg
By the way , to compile it you should have run this:
javac -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
This should work fine if you compiled the FacebookProg.class in same directory. You can try java -cp /usr/share/java/scribe-1.3.0.jar:/locationOfFacebookProg.class directory/ FacebookProg
This
java -cp /usr/share/java/scribe-1.3.0.jar FacebookProg
means you are running the FacebookProg class, not compiling it.
If you leave the -cp ... out, you are leaving the vital classpath out, so the JVM cannot find the classes FacebookProg requires.
To compile, you need
javac -cp /usr/share/java/scribe-1.3.0.jar FacebookProg.java
(note the javac, instead of java to invoke the compiler)
To run, you already know how to.
Also, you have errors in the follwoing lines:
export CLASSPATH="/usr/share/java"; java FacebookProf
export CLASSPATH="/usr/share/java/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
The first misspells FacebookProg and does not have the required jar on the classpath, the second has the wrong path to the jar. Try
export CLASSPATH="/usr/share/java/scribe-1.3.0.jar"; java FacebookProg
Also, make sure the jar is indeed located at /usr/share/java/scribe-1.3.0.jar
suppose that, the directory of your program is
$HOME
$HOME/lib/*.jar
you can write a script like:
for file in "$HOME/lib/*.jar"
do
if [ -f $file ]
then
CLASSPATH=$CLASSPATH:$file
else
echo ignore $file
fi
done
java -cp $CLASSPATH FacebookProg

Unable to run compiled .classes from the command line

I am having some issues running my compiled java code from the command line. I have written it and compiled it using the IntelliJ IDE (where everything runs fine if done within the IDE), but wish to now run it from the command line.
Compiling from the command like (using javac) also works fine, but running (with java) does not.
I am almost certain this is a classpath issue but cannot seem to fix it. From my searching prior to posting this I found a post telling me to run the "set PATH=\%PATH\%;"C:\Program Files\Java\jdk1.6.0_21\bin" command and then try running java. I have also tried various arguements I have found for -cp and -classpath. The error is :
Exception in thread "main" java.lang.NoClassDefFoundError: Share/class
Caused by: java.lang.ClassNotFoundException: Share.class
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: Share.class. Program will exit.
You're doing:
java -cp ... Share.class
Do
java -cp ... Share
Or if it's in a package
java -cp ... path.to.Share
You should not be supplying the class file as an argument, you should be supplying the fully qualified class name.
If your class is in the current directory and uses the default (empty) package, it will just be
java -cp . Share
or
java Share
The classpath is not used to point to the java executable, it's used to point to the various directories/jar files which contain your class files (at the root of the package structure).
See also
java - the Java application launcher (manual for invoking java)

ClassNotFoundException thrown when compiling basic Hello World Program

I am just trying to compile and run a very simple test program, but it simply will not work, and I have no idea what the problem is.
I have a java project that's been heaped on me, and I know little to nothing about java. Especially compiling from the windows command line.
I have two Jars that I need to compile a simple "hello world" program with.
Here's my "build.bat"
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
Then, I:
C:\jdk1.6.0_21\bin\java sample
This spits back the error:
Exception in thread "main" java.lang.NoClassDefFoundError: sample
Caused by:
java.lang.ClassNotFoundException:
sample
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:
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
No matter how I set up my classpath, I cannot for the life of me get this HELLO WORLD program to run.
Can someone please help me out? I'm pulling my hair out.
You can also specify the classpath for the interpreter to locate your class:
java -classpath "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; Sample
This would run your class from any working directory.
Run your program from the directory with the class in it:
C:\Users\FREYERA\Desktop\Test\>java sample
CLASSPATH (normally) includes the current directory.
If you have "sample.class" in the current directory, and you also need classes in test1.jar and test2.jar, this should work:
java -cp "test1.jar;test2.jar;." sample
Afer running this command
C:\jdk1.6.0_21\bin\javac -cp "C:\Users\FREYERA\Desktop\Test";"C:\Users\FREYERA\Desktop\Test\test1.jar";"C:\Users\FREYERA\Desktop\Test\test2.jar"; "C:\Users\FREYERA\Desktop\Test\sample.java"
It would have created a .class file at this location "C:\Users\FREYERA\Desktop\Test\". i.e. sample.class
You need to either go to this folder location and run your java command to execute the program. Make sure that your "JAVA_HOME" environment variable is set.
Or you can copy the sample.class file to "C:\jdk1.6.0_21\bin\" folder and run the command.

Categories

Resources