NoClassDefFoundError when running from the comand line - java

I would like to use an external library (e.g. Google's Guava) for my Java program. I use Eclipse, so I downloaded Guava's jar (and source) and followed Adding a Java library to the project classpath to add it to Eclipse and to the buildpath of my project.
This works fine: I can run the program from Eclipse and from the runnable jar I export from Eclipse, but I get an error when I try to run directly from the bin/ dir, as I used to do before:
Exception in thread "main"
java.lang.NoClassDefFoundError: com/google/common/base/Joiner
What should I do?

If you're running the class file directly from the project bin directory then you may have to specify the classpath manually:
C:> java -classpath C:\java\MyClasses;C:\java\OtherClasses MyClassHere

Youll have to tell Java where to find the library:
java -cp <path-to-lib-jar>;myJar.jar my.package.MyMainClass
or if you wanna use a jar file you can set the library path in the MANIFEST
check here for an explanation.

Have you tried java -cp guava.jar ... ?

To run the program on console as precise as possible with when you run it from Eclipse, you need to run it from the root directory of the project (not from bin) and don't forget to mention the classpath (http://download.oracle.com/docs/cd/E17476_01/javase/1.5.0/docs/tooldocs/windows/classpath.html)
So for example on root you will run:
java -classpath lib/guava.jar;bin packageName.className

Related

java Could not find or load main class from eclipse compiled code

I have compiled my java code using eclipse but not it has to be deployed and a cron job has to execute it. I am trying to execute it from command line in Windows, but getting Could not find or load main class. I tried setting classpath using java -cp bin\com\pega\download\engineclasses but it still throws the same error. My folder structure looks like below
C:\Users\s2517457\G360_Linux\FiddlingPega
|__\bin\com\pega\download\engineclasses\TestUtils.class
|__\src\com\pega\download\engineclasses\TestUtils.java
Please let me know what should be the javac and java commands for this to work.
You should use the following command:
java -cp bin/ com.pega.download.engineclasses.TestUtils
Your are telling to java that the entire bin folder is your classpath and the main class is in the class com.pega.download.engineclasses.TestUtils
If you want to add jars as well, you must call the command like:
java --classpath "bin/;lib/*" com.pega.download.engineclasses.TestUtils
Where lib is the folder containing the Jars files

External jars not loading in built project. NoClassDefFoundError

So, I have exported my project in both Netbeans and Eclipse and when I try to
java -jar myproject.jar
I get this prompt
In my project I have some libraries which are located inside of src in Netbeans and out of src in Eclipse as it should (please correct me if I'm wrong) The libraries are included via:
Java Build Path > Add JARs...
I've done some research and it seems that I have to change my JAVA CLASSPATH or somethng like that but I don't know exactly how to do it.
The project works perfectly when I compile it and run it, but it crashes after I build it into a Jar file.
By the way, if it isn't clear enough I'm on Ubuntu 14.04
You need to create the path for the jar files and pass it on the command line.
Something like this:
ftp_jar=${Utils_home}/bin/ftpClientUtil.jar
net_jar=${Utils_home}/bin/commons-net-3.3.jar
jsch_jar=${Utils_home}/bin/jsch-0.1.51.jar
java -cp .:$jsch_jar:$net_jar:$ftp_jar com.myplace.ftputils.SFTPClientUtil $*
Run your program as:
java -cp .:[path-of-lib1.jar]:[path-of-lib2.jar] -jar myproject.jar
replace [path-of-libX.jar] with actual path of your libraries.

Launching Java .class with External Libraries in Terminal

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.

class not found error in java when compiling a project with libraries

I have created a project in eclipse and added jdbc jar library successfully.When I run the program in eclipse I see no error but When I type:
javac *.java
in console no problem occur but later when I type
java Runner
I get "java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
and more errors related to this."Runner" is the class which has the main method. But in eclipse eveyrthing was working fine.
This means com.mysql.jdbc.Drive is not on your classpath at runtime when you run your program outside of Eclipse. Add it to your classpath.
Try java -classpath or java -cp.
Try also java -help for help and additional details.
See also: http://reins.altervista.org/java/A_Collection_of_JVM_Options_MP.html
You need the jar file at run time as well.
Use java -cp pathToYourJar Runner
Because eclipse already has a classpath, when running from command line you need to specify it also.
As the others say, you need to pass to javac command the classpath, the way to do it is explained in the following question
Including jars in classpath on commandline (javac or apt)

creating our own jar and using it in another project

I want to create our own jar which has some simple methods like a print method.
I created the jar of that java project and imported it into an other project I am building path also.
The problem is that I am able to create the object of the classes which reside in the jar but I am unable to call the methods of that class.
i am using eclipse 3.4 (Eclipse Ganymede (3.4))version
Sounds like if you are successfully building the JAR that you are not including it in the classpath when you compile / run your application. You can do that if you are compiling/running from the command line with the -cp or -classpath option. Both perform the same function.
To compile:
javac -cp .:PathToMyJar/MyJar.jar MyMainClass.java
To Run:
java -cp .:PathToMyJar/MyJar.jar MyMainClass
The above commands will look in the current directory ('.') and in the MyJar.jar file specified. On Windows you will separate the items in the classpath with a semicolon and on Linux/Unix/OS X you will use a colon.
If you are using an IDE you will need to somehow add the JAR file to the classpath for your project. How to do that is IDE specific.

Categories

Resources