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
Related
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
I am able to use:
C:\Program Files (x86)\Java>javac -classpath mail.jar myFile.java
to compile,but when I try to run it:
C:\Program Files (x86)\Java>java -cp mail.jar myFile
Exception in thread "main" java.lang.NoClassDefFoundError: myFile
Caused by: java.lang.ClassNotFoundException: myFile
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)
Could not find the main class: myFile. Program will exit.
I tried using various combinations using "." and "-classpath" to denote class path as other posts have sugested but couldn't figure it out. I've compiled and successfully run a basic helloworld.java in the same folder. running as admin. using java runtime 1.6.
Put your current directory into the classpath as well:
java -cp .;mail.jar myFile
If that doesn't work, post myFile.java so we can see what might be going on. For example, is myFile in the default package or is there a package statement?
You need to do one of the following:
pack the jar first (use the jar command) and use java -jar
not specify a classpath and just run your class file (standalone applications)
(if the jarfile is a library) you need to include the current directory in your classpath
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
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)
I'm working through a ANTLR (a language processing library) book and there are many examples that should be easy to compile using the command line.
Some information to get te problem:
antlr-3.2.jar contains the library classes. I added the antlr-3.2.jar to the CLASSPATH environment variable (Windows 7) and when compiling the classes with javac everything works fine.
This is what i execute to compile my program:
javac Test.java ExprLexer.java ExprParser.java
Test.java contains my main()-method whereas ExprLexer and ExprParser are generated by ANTLR. All three classes use classes contained in the antlr-3.2.jar. But so far so good. As I just said, compiling works fine.
It's when I try to execute the Test.class that I get trouble.
This is what I type:
java -cp ./ Test
When executing this, the interpreter tells me that he can't find the ANTLR-classes contained in the antlr-3.2.jar, altough I added an entry in the CLASSPATH variable.
Exception in thread "main" java.lang.NoClassDefFoundError: org/antlr/runtime/Cha
rStream
Caused by: java.lang.ClassNotFoundException: org.antlr.runtime.CharStream
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: Test. Program will exit.
I'm using Windows 7 and Java 1.6_20. Can someone tell what is going on? Why will the interpreter not look in the jar-Archive I specified in the CLASSPATH?
I found some kind of workaroud. I copied the antlr-3.2.jar into the directory where the Test.class is located and then executed:
java -cp ./;antlr-3.2.jar Test
This worked out. But I don't want to type the jar-Archive everytime I execute my test programs. Is there a possibility to tell the interpreter that he should automatically look into the archive?
I'm using Windows 7 and Java 1.6_20.
Can someone tell what is going on? Why
will the interpreter not look in the
jar-Archive I specified in the
CLASSPATH?
-cp on the commandline overrides the CLASSPATH variable. There is no convenient way to do what you're trying to do. I'd suggest creating an ant script, shell script, or shell alias if you don't want to type out the full classpath each time.
Alternatively, you could put your Test application into its own jar file with a manifest that tells it to include antlr-3.2.jar in the classpath.