I've looked around and nothing has solved my problem.
I'm trying to launch a .class file within terminal while including another directory. This is how I'm doing it:
java -cp lib/*:bin/ org.package.file
However I keep getting an error essentially stating that I'm not including the lib directory.
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/Lists
The file is bin/org/package/file and I need to include the entire lib directory.
I'm doing this in Terminal on an Ubuntu OS.
You will need to specify individual libraries in the command line to specify the classpath rather than using wildcard like:
java -cp lib/a.jar:lib/b.jar:bin/ org.package.file
From the looks of it, you do have guava jar in your lib directory, so try to include all the jar's in your lib directory like above and that should resolve your issue.
Related
So I followed a couple of tutorials and I am basically trying to run a java program on cmd line with an External Jar. I know that there are plenty of questions about this, BUT after trying the code suggested I get two errors.\
Its a simple program called "HelloJar.java" and it utilized Apache Commons Lang String Utils.
Error I see (As you can see I set the classpath and run it.):
Works fine in Eclipse though:
Folder Structure (In jars is the jar necessary and in Src is the project I need):
Here is the .jar file that I import for StringUtils:
What does the cannot find symbol mean?
-----------------------After Rajesh's Answer------------------------------------
Problem here is, jar's not set in classpath and results in compilation errors:
As per path shared, command to compile should be :
cd C:\Users\Controlled\Documents\Eclipse_Projects\HelloWorld\src
javac -cp .;C:\Users\Controlled\Documents\Eclipse_Projects\HelloWorld\jars\* HelloJar.java
Command to run java program with above path:
java -cp .;C:\Users\Controlled\Documents\Eclipse_Projects\HelloWorld\jars\* HelloJar
What does the cannot find symbol mean?
That means, compiler is unable to find the specified path in the classpath. So, that means, when you are trying to run your jar from command line, your library which has
org.apache.commons.lang.StringUtils
is not found. So, in order to fix that, please check if the library is included in your jar or if you have that in classpath and specifying the correct classpath.
Although you did set your classpath to the correct folder, windows fle path separation is different from a Unix Box.
javac -cp ".;./jars/org.apache.commons.lang.StringUtils.jar/" HelloJar.java
java -cp ".;./jars/org.apache.commons.lang.StringUtils.jar/" HelloJar
Another Java distinction is that the classpath is the compile-time path. It is not the same as the execution-time path.
I have been doing a coding exercise inside the IntelliJ IDEA Community Edition 14 IDE, using OpenJDK.
The project is split over 4 .java files all in the same package.
My end goal is to run this in the terminal/bash (I use System.console().readLine() which doesnt play nicely in the IDE's console).
I've tried navigating to the directory where these 4 files reside (they all reside in the same dir) and tried:
javac -classpath . BibliotecaApp.java Book.java BookManager.java LibraryDB.java
This creates 4 corresponding .class files fine.
The Main is in bibliotecaApp.java/class, so I try run it by:
java BibliotecaApp
But I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: BibliotecaApp (wrong name: com/twu/biblioteca/BibliotecaApp)
Plus about 13 lines of specifics.
Now googling this error seems to be a class path problem, and this is where I get stuck.
From places I've read, usingecho $PATH gives me:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
This is still from the directory of the .java files above.
That path means nothing to me. I have no idea what it is or what it does, or what even a classpath is! Theres alot of resources out there on setting a classpath, but they aren’t helping me because I don't know what it's meant for.
That was a dead end for me. I tried to create a .jar instead using IDEA's Build Artifacts as seen HERE. It created a .jar file but when I navigate to that directory and try run it via:
java -jar biblioteca_jar
I get
Error: Invalid or corrupt jarfile biblioteca_jar
Another issue is that in the file explorer, the file actually comes out as biblioteca.jar, but ls on that dir shows biblioteca_jar. Is that normal?
The code is on my GitHub if that helps anything
https://github.com/botagar/twu-biblioteca-JohnGeddes
Based on your compiler step, change this
java BibliotecaApp
to
java -cp . BibliotecaApp
Which will add the current directory to the classpath for the java runtime environment. See also the Oracle Technote Setting the Class Path.
A jar file is a kind of zip, and should have a .jar extension. So this
java -jar biblioteca_jar
should probably be
java -jar biblioteca.jar
And you can check if the file is valid with any zip archive reader. Including jar itself,
jar tvvf biblioteca.jar
Edit
Based on your comments below,
cd ~/Documents/ThoughtWorks Uni/TWU_Biblioteca-master/src/
and then
java -cp . com.twu.biblioteca.BibliotecaApp
I have exported a jar file that I want to run the console. The code compiles and runs correctly in eclipse but I am having an issue running it from the console.
To me it looks like the referenced jar's I added via built path in the Eclipse project file and not being added to the export. If that is the case, how do I ensure that they do? If not, what am I doing wrong?
When you export your source code's class files to a jar using eclipse, only the .class files of your source are exported! Hence your exported jar file doesn't contain the referenced jars you mentioned in eclipse! Due to this, the error occurs while executing from command prompt.
Solution:
Take all the jar files required to execute the program, store it in the same directory as you store the exported jar file. Now while executing the java command, provide all the jar file's names in classpath field as following:
java -classpath .;JAR1.jar;JAR2.jar MainClass
Once you do this, your problem should be resolved!
The dependencies need to be on the classpath, i.e. run like this:
java -cp <path_to_jar1>;<path_to_jar2> -jar ScrumTimeCaptureMaintenence.jar
When running from the command line make sure any dependencies are set on the class path by listing them in the -classpath parameter
I've got a weird problem that I can't understand... I have a simple HelloWorld jar that I built in Eclipse which has the Apache Loggings jar on it's classpath. I've written a script to run the jar:
#!/bin/sh
export CLASSPATH=lib/*:$CLASSPATH
java -jar HelloWorld.jar
The directory structure here is a main directory with the HelloWorld.jar and a lib subdirectory holding the commons-logging-1.1.1.jar.
Running this script works fine. However, when I place the HelloWorld.jar into the lib directory (i.e. to contain all the JARs in one place), and executing java -jar lib/HelloWorld.jar, I get:
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
error. Why?!?!?!?!
I'm asking this because I've used the JarBundler on OSX to create an Application bundle for the HelloWorld app and placed a modified script in the MacOS directory whereas all the libs are placed in the Resources/Java directory. Modified version of the script is:
#!/bin/sh
RESOURCE_DIR=$(cd "../Resources"; pwd)
export CLASSPATH=$RESOURCE_DIR/Java/*:$CLASSPATH
java -jar $RESOURCE_DIR/Java/HelloWorld.jar
and I'm getting the same error as above I'd really appreciate any help understanding why I can't do this and/or how to fix it?
Classpath doesn't work with wildcards. Every jar has to be specified explicitly, either as part of the CLASSPATH variable or in the manifest of another jar that is included in the classpath.
Also, IIRC java -jar ignores all the third party jars that are present in the classpath. Why not do this instead?
java -cp yourJar:logJars <mainClass>
Try to add the commons-logging-1.1.1.jar to the CLASSPATH directly
Java will not work with lib/* but the shell may be expanding it for you. Double check this. Put a line like this after export:
echo $CLASSPATH
Also, I would recommend putting it in the MANIFEST file as already mentioned.
EDIT:
Is it a permission problem? If you run the app as root/admin or put the file somewhere else and use a fully-qualified path does it work?
Use the MANIFEST file (META-INF folder) to deal with Classpath entries. Use relative paths for the libraries.
For further info, take a look here.
Basically, for the case with commons-logging inside a lib folder:
Class-Path: lib/commons-logging-1.1.1.jar
And for both jars in the same folder:
Class-Path: commons-logging-1.1.1.jar
Thanks to everyone for their help in figuring this out. Basically, a manifest file was being created and bundled into the jar without my knowledge so any $CLASSPATH or -cp flags were being ignored. In my Eclipse project, I had my classpath set to $(projectRoot)/lib which coincidentally the same directory structure as my dist directory. However, when they were bundled into one directory by OSX's JarBundler, the directory was no long present, hence the classpath errors!
I tried removing the Class-Path attribute from the MANIFEST.MF that Eclispe created but the command line $CLASSPATH and/or -cp entries still don't seem to make a difference... Does the existence of a manifest file negate all command line classpath entries?
have you set log4j.jar into your class. i think you didnt added log4j.jar of to its class path.
I've looked through many of the existing threads about this error, but still no luck. I'm not even trying to package a jar or use any third-party packaging tools. I'm simply running from within Eclipse (works great) and then trying to run the exact same app from the command line, in the same location it's built to (getting this error). My goal is to be able to zip up the bin folder and send it off to be run by someone else via a command line script. Some details:
It's a command-line app and I'm using the commons-lang-2.4.jar for string utilities. That is the file that cannot be located (specificaly "java.lang.NoClassDefFoundError: org/apache/commons/lang/StringEscapeUtils")
I have that jar in my lib folder and have added it to my build path in Eclipse via right-click "Build Path -> Add to Build Path"
The .classpath file looks correct and contains the reference to the jar, but I assume that file is only used by Eclipse (contains this line: <classpathentry kind="lib" path="lib/commons-lang-2.4.jar"/>)
Could this be related to the Eclipse working directory setting? I have some internal template files that I created that are under src/templates, and the only way I can seem to get those to be seen is by setting the project working directory to AppName/src. Maybe I should be putting those somewhere else?
Let me know if any additional info would help. Surely this is something simple, but I've wasted too much time on it at this point. This is reminding me why I originally left Java back in '05 or so...
A NoClassDefFoundError basically means that the class was there in the classpath during compiletime, but it is missing in the classpath during runtime.
In your case, when executing using java.exe from commandline, you need to specify the classpath in the -cp or -classpath argument. Or if it is a JAR file, then you need to specify it in the class-path entry of its MANIFEST.MF file.
The value of the argument/entry can be either absolute or relative file system paths to a folder containing all .class files or to an individual .jar file. You can separate paths using a semicolon ;. When a path contains spaces, you need to wrap the particular path with doublequotes ". Example:
java -cp .;c:/path/to/file.jar;"c:/spacy path/to/classes" mypackage.MyClass
To save the effort of typing and editing the argument in commandline everytime, use a .bat file.
Edit: I should have realized that you're using an Unix based operating system. The above examples are Windows-targeted. In the case of Unix like platforms you can follow the same rules, but you need to separate the paths using a colon : and instead of an eventual batch file, use a .sh file.
java -cp .:/path/to/file.jar:"/spacy path/to/classes" mypackage.MyClass
Are you specifying the classpath to java on the command line?
$ java -cp lib/commons-lang-2.4.jar your.main.Class
The classpath setting you are setting in Eclispe are only for the IDE and do not affect how you application is run outside the IDE. Even if you use the Eclipse Functionality to export your application as an executable jar file there is no out of the box way to package all the jars your application depends on.
If you have packaged you application into a jar file called myapp.jar then running a command like below will run the application with the jar you depend on, if you have more than one just add them separted by ; on Windows or : on Unix:
java -jar myapp.jar -cp .;c:/pathtolibs/commons-lang-2.4.jar
If you are just running the classes directly then either run the folder containing your .class files will also need to be on the path (though I assume it already is since you are able to run the program and get errors).
Consider File -> Export -> Runnable jar to create a jar file which can be invoked directly with
java -jar yourProgram.jar
There are several variants depending on your needs.
Eclipse does not move any of the jars in your classpath into the bin folder of your project. You need to copy the util jar into the bin folder. If you move it to the root of the bin folder, you might be able to get away without any classpath entries but it's not the recommended solution. See #BalusC's answer for good coverage of that.
Eclipse doesn't build executable java classes by default. Don't ask me why, but it probably has something to do with using their own tools.jar (somewhere in plugins/org.eclipse.core ?) so that Eclipse can run without a JDK.
You can usually go to your project bin directory and do:
java -cp . MyClass
But if you have external jars, Eclipse handles those internally in another weird way, so you'll need to add those too.
make sure your jar commons-lang-2.4.jar in classpath and not redudance.
I ever add jar file to my classpath, and have 2 file jar in my classpath. After I delete it, work smooth