Why jar doesn't work with command line? - java

I'm making a project to modify a .wav file to .flac file, I use the class http://javaflacencoder.sourceforge.net/javadoc/index.html. My project in IDE eclipse works well. But when I exported it into .jar and run it with command line on linux. There is the error:
Exception in thread "main" java.lang.NoClassDefFoundError: javaFlacEncoder/FLAC_FileEncoder
at speech.Speech.main(Speech.java:44)
Caused by: java.lang.ClassNotFoundException: javaFlacEncoder.FLAC_FileEncoder
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
Without idea, I can't understand why. Could anybody have the same problem? Could you show me how to resolve it? Thanks,

You should probably add a classpath entry to let Java know of your librarie.
Typically, you must do:
# generate the classpath entry
declare classpath=$(for jar in lib/*.jar; do echo ":$jar"; done)
# remove the first character (':').
classpath="${classpath:1}"
java -cp "${classpath}" -jar yourjar.jar
Or:
# the same code that generate classpath
export CLASSPATH="${classpath}"
java -jar yourjar.jar
Also, in the manifest of the jar, you can list them the Class-Path entry.

Eclipse takes care of setting up the classpath for the execution of your Java program; you probably failed to replicate this on the command line. There are several options:
if starting with java <main_class_name>, then add the -cp argument listing all the dependency JARs;
if starting with java -jar <jar_name>, then make sure the JAR's manifest (MANIFEST.MF) includes the Class-Path entry, which lists the other JARs it depends on;
create an "uberjar" which contains all the necessary classes. Maven's shade plugin can do that for you.

Related

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 commandline including .jar

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

No main found when executing jar

http://pastebin.com/1btVw8Cb
There are two classes in the above code.
So above is my code which is working fine when I hit run in Eclipse, runs fine in Netbeans as well.
I am trying to create a standalone application, a jar file.
The error I get when I double my jar is:
Could not find the main class: NewJFrame. Program will exit.
I get the following from the command promt:
E:\Java Programs\Eclipse Workspace\test3\src\test3>java testT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: testT/jar
Caused by: java.lang.ClassNotFoundException: testT.jar
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: testT.jar. Program will exit.
I followed the command from here: "Could not find the main class: XX. Program will exit."
So this is what I am typing in the command promt to create my jar:
E:\Java Programs\Eclipse Workspace\test3\src\test3>jar cfm MyJar.jar manifest.tx
t *.class SINGLE.TXT
http://imgur.com/a/wlCpc#SYR3L
Some shots above I took to show the process.
So I think the problem could be error when I do javac? But it builds fine and runs fine in netbeans and eclipse >< Please help.
You need to use the -jar flag.
java -jar MyJar.jar
http://docs.oracle.com/javase/tutorial/deployment/jar/run.html says
JAR Files as Applications
You can run JAR-packaged applications with the Java interpreter. The basic command is:
java -jar jar-file
The -jar flag tells the interpreter that the application is packaged in the JAR file format. You can only specify one JAR file, which must contain all the application-specific code.
and http://docs.oracle.com/javase/tutorial/deployment/jar/modman.html explains how to put a manifest in the jar.
The m option indicates that you want to merge information from an existing file into the manifest file of the JAR file you're creating.
it is now working. I have not changed anything, maybe the memory got curroupted and that is why it was giving me problems. I think restart the computer is what fixed it. But yea, I don't know for sure what was wrong, though it is working fine now. So thank you for your help guys
Add
Main-class: Splash
part to Manifest file. set Main class name instead Splash
That will work...

Jetty Classpath issues

I'm currently running Solr out of a Jetty container that it ships with. It runs correctly when run from the command line via:
java -jar start.jar
when I'm in the same directory as start.jar. Unfortunately I need to be able to launch jetty from any directory, not just the one that contains start.jar. I've tried many options, such as:
java -Dsolr.solr.home=~/solr/ -Djetty.home=~/solr/ -Djetty.logs=~/solr/logs/ -cp ~/solr/start.jar:~/solr/lib/jetty-util-6.1.26-patched-JETTY-1340.jar:~/solr/lib/jetty-6.1.26-patched-JETTY-1340.jar:~/solr/lib/servlet-api-2.5-20081211.jar -jar ~/solr/start.jar ~/solr/etc/jetty.xml
Every time I get this backtrace:
java.lang.ClassNotFoundException: org.mortbay.xml.XmlConfiguration
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 java.lang.ClassLoader.loadClass(ClassLoader.java:266)
at org.mortbay.start.Main.invokeMain(Main.java:179)
at org.mortbay.start.Main.start(Main.java:534)
at org.mortbay.start.Main.start(Main.java:441)
at org.mortbay.start.Main.main(Main.java:119)
Simply changing to the correct directory before calling java.... fixed the problem for me.
Note that when you run
java ... -cp ~/solr/start.jar:... -jar ~/solr/start.jar ~/solr/etc/jetty.xml
the -cp option is ignored since you use the -jar option.
From man java:
-jar
When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.
You have two options:
Keep using the -jar option, but then you need to provide the classpath in the jar manifest file (note that these classpath entries can't be relative to the current path, only relative to the jar-file you're executing)
Skip the -jar option and provide the main class explicitly.
You're using the ~ as a short cut to the current user's home directory. I'd replace all tilde characters with an absolute path and see if that helps.
I ran into this in Jan 2014.My issue was that because I ran a Cluster Zookeeper setup from elsewhere, the $SOLR_HOME/lib folder got moved under $SOLR_HOME/cloud-scripts where the zkCli.bat exists.Copied the lib folder back under $SOLR_HOME/ and it works now.

Jar not found when executing class

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.

Categories

Resources