Java program with referenced libraries: NoClassDefFound [duplicate] - java

This question already has answers here:
NoClassDefFound when trying to run java with external libraries
(4 answers)
Closed 7 years ago.
How do I run a program in Java, with several referenced libraries, .jar files, from a command line?
I have put all my .jars in /lib folder, which is in the root folder of my project, and added those .jars to the build path.
So my project now looks something like this:
Project:
-->/src/Entry.java, ... (all them .java files)
-->/bin/Entry.class, ... (all them other .class files)
-->/lib/commons-codec-1.10./(all them .jars)*
Now when i try to run the program from the cmd:
I locate myself within the /bin folder and execute java Entry, but I get NoClassDefFound exception
How should I run this?

you need to tell JVM where to look for classes while running the program.
the parameter that we use to tell jvm that is known as classpath
there are different ways to achieve that
Recomended Add the classpath location to the run command , alternatively pass the jar locations, assuming you have two jar files a.jar and b.jar under you lib folder, the command should be java -cp".;lib/a.jar;lib/b.jar" Entry
Either put the jar files into a location that is already under classpath (Since current folder is always under classpath, easiest option would be to put the jar under current folder , but this is not a recomended way to achieve)
Modify you classpath variable under environment properties to list the folder containing your jar , which is a trivial way for achieving this.

Create one batch file (.bat) and keep the jars inside that. whenever you want to run then directly run that batch file. I guess its very simple and efficient.
Example:
#echo off
SET PATH=%PATH%;E:\Java\jdk1.6.0_45\bin // JDK path
SET LIB=%cd%\lib
set CP=""
set CP=%CP%;%LIB%\antlr-2.7.6
set CP=%CP%;%LIB%\commons-codec-1.8.jar
set CP=%CP%;%LIB%\opencsv-2.3.jar
javac -classpath %cp% *.java
java -classpath %CP% -Xms256m -Xmx1024m -Xss2m T2DPreProcessing
pause
I hope it will help you. Thanks.

Related

How to include JAR files on a batch file [duplicate]

This question already has an answer here:
Including Jar while executing Java from Bat file
(1 answer)
Closed 4 years ago.
This might be similar to THIS but I can't find a solution written there
Problem is, I am running a java app thru a batch file which needs some jar files to be included. And I can't find a way to do it
File locations:
(java class)
D:\workspace\src\MyClass.java
(jars needed)
D:\workspace\src\lib\opencsv-4.1.jar
D:\workspace\src\lib\common-lang3.jar
Current code on bat file:
#echo off
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;D:workspace\src
set CLASSPATH=%CLASSPATH%;\lib\opencsv-4.1.jar
set CLASSPATH=%CLASSPATH%;\lib\common-lang3.jar
set ARGS=one two
javac D:workspace\src\MyClass.java
java -cp %CLASSPATH% MyClass %ARGS%
pause
Please help on how to properly include these jar files to be able to run the program. Thanks
Please help on how to properly include these jar files to be able to run the program
Don't start from here. You should:
Deploy your application as a JAR file, naming the main class in the META-INF/MANIFEST.MF file's Main-class entry.
Name the external JAR files in the META-INF/MANIFEST.MF file, in the Class-Path entry. Note that this only allows you to use relative pathnames to the other JAR files, so you essentially have to distribute them with the application JAR file.
Use the java -jar option.
In which case you barely need the batch file at all.
Your relative and absolute path in your script file is not valid...
Especially the relative path in your script...The java file location must be either relative path or absolute path.
You don't need to use the '-cp' flag since you set the CLASSPATH variable.
So, if your jars path is a lib directory under workspace in a D volume.
The script looks like as follows;
#echo off
set CLASSPATH=.
set CLASSPATH=%CLASSPATH%;D:\workspace\src
set CLASSPATH=%CLASSPATH%;D:\workspace\lib\opencsv-4.1.jar
set CLASSPATH=%CLASSPATH%;D:\workspace\lib\common-lang3.jar
set ARGS=one two
cd D:\workspace\src\
javac MyClass.java
java MyClass %ARGS%
pause

How do I generate .class file if I have external Jar dependencies? Javac does not work [duplicate]

This question already has an answer here:
How do I compile a java file that has jar dependencies?
(1 answer)
Closed 8 years ago.
Background: Running on an ec2 instance not eclipse.
I have a java program that has external Jar dependencies. I want to create an executable jar file for this java program (being able to package the external jar files into this jar file would be icing on the cake)
The problem is that for me to use the java cf or cmf command I need to have a .class file. But, when I try javac HKP.java it throws a bunch of errors saying "xyz does not exist or cannot recognize symbol" which I'm guessing is because it has no idea about the external jar dependencies.
I have tried doing javac -sourcepath /home/ec2-user/apps/HKP/lib/ HKP.java, but it throws the exact same errors as before.
EDIT I have tried javac -classpath file1.jar, file2.jar, file3.jar HKP.java -I have also tried this with ";" in between. both return errors saying -bash file2.jar command not found, so instead of continuing the argument, bash is recognizing the multiple jar files as commands.This is the initial problem I had that caused me to post here.
Any help is much appreciated. Thank you!
If you want to compile a java source file into a class, you need to provide all classes which are used in that code on the classpath of the compiler.
If the classes come from (already compiled) external JARs, you typically do that by specifying javac -classpath with a list of JAR files, separated by : (on Linux). But you should really think about using an IDE or at least a maven build file (Which has the benefit, it can even download those JARs for you).
Both (Eclipse IDE and Maven build system) can also generate a ueber-jar with the external classes in there for easy execution.
try to Add the path in system variable and restart your computer.

Java CLASSPATH Not working?

I am trying to use other jars for a program I am writing.
I installed all the required files and added them to CLASSPATH, but Java doesn't recognize the packages.
I put semicolons in between the jar locations but Java doesn't recognize the packages from the jar. Why does that happen?
For example,
my classpath looks like:
.;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Users\JOE\Downloads\easymock-3.2\easymock-3.2\easymock-3.2.jar;C:\Users\JOE\Downloads\cglib-3.1.jar;C:\Users\JOE\Downloads\objenesis-2.1-bin\objenesis-2.1.jar
but if I try to import package org.easymock.EasyMock for example, the package is not recognized.
If your trying to load dependencies from external jars, specify the paths using -classpath (or) -cp command line argument. It's not ideal to change the CLASSPATH environment variable for each and every program that you execute.
The default ClassPath for java program is dot (.) which means current directory
Remember that when you're using -cp/-classpath arguments, they'll override the default classpath settings, so you should explicitly add the default path as well like below.
On Windows
javac -cp pathToYourJar Main.java
while executing don't forget to add your current directory
java -cp .;pathToYourJar Main
To make things easier, I'd recommend to use an IDE like Eclipse/NetBeans/IntellijIDEA.
If your already using Eclipse, add the jars to your Project's Build Path
Right Click on Project -- Properties -- Java Build Path -- Libraries -- Add External JARs

Error when executing a JAR file

I've been learning about JAR files and wanted to try and create and run one myself. I carried out the following steps:
Created a project folder with a 'source' subfolder and a 'classes' subfolder
I wrote 2 source files, one with a main method which creates an instance of the other class and runs a simple method in it.
Compiled these to the 'classes' subfolder. I checked to see if they would run. They did
I created a manifest.txt file and filled in the Main-Class: xxxx and hit the return key. I saved this in the sources subfolder
Created a jar file in the classes subfolder by writing
jar -cvmf manifest.txt zzz.jar *.class
Tried to execute the jar file by typing
java -jar zzz.jar
This gives a ClassNotFound exception. If I try to execute the jar by double clicking on it in windows I get an errorbox saying "Could not find the main class xxxx"
I've double checked the spelling of the class inside the manifest file and it's correct.
Possibly important: I have to compile my programs using java -cp . xyz as there is an issue with my classpath. Does this mean that I need to execute jars in a different way as well? I tried
java -cp . -jar zzz.jar
but ended up with the same exception.
Edit: I ended up starting from scratch and now it runs (with the basic -jar zzz.jar command). Frustrating that I don't know what I was doing wrong but glad that it is working!
Shouldn't number 5. be run in the classes subfolder, where all your class files are? And if your classes are in packages, which they should be, you'll likely want to use * instead of *.class..?
To check what your jar file contains you can run:
jar tf zzz.jar
You will probably have to supply the entire path of the .class file you wish to execute after the classpath. ie java -cp xxx.jar classes.mainProgram.class. Where classes is the name of the folder which contains your class files.

Java - Difficulty installing program from 3 separate .jar files (involves CLASSPATH)

I'm having a little trouble running some Java code, which requires three .jar files to be used. I'm at a lost as to what to do with them--I've tried setting the CLASSPATH (and following the instructions for how to do so in the readme files), but to no avail.
I was wondering if someone could walk me through it? I'd imagine three .jar files would be an easy install for someone who knows what they're doing.
If it helps, I'm using Ubuntu pretty much right out of the box (but I do have JDK and Eclipse installed!)
Runtime library: http://cogcomp.cs.illinois.edu/download/software/20
Additional .jar needed: http://cogcomp.cs.illinois.edu/download/software/23
Program I ultimately need to run: http://cogcomp.cs.illinois.edu/download/software/26
If you're willing to help, I can't thank you enough--you deserve a million kudos!
G
Those are all JAR files. When you execute a JAR file by doubleclicking or using java -jar, the CLASSPATH environment variable and the -cp and -classpath arguments are ignored. The classpath should be defninied in META-INF/MANIFEST.MF file of the JAR. In this particular case, only the second and third JAR have a Class-Path entry in the manifest file:
Class-Path: LBJ2Library.jar
Which is the first JAR. The classpath is telling that it is expecting the LBJ2Library.jar to be in the same folder as the JAR you'd like to execute (either the second or third one).
So, just drop them all in the same folder and execute by java -jar LBJPOS.jar.
If you are using java -jar to run your jar files, then the CLASSPATH variable is ignored. If you are using java -jar, you have two options:
Combine the three jars into one jar.
Run the main class directory and don't use -jar.
Use of the CLASSPATH environment variable is generally discouraged nowadays. This is how it's done (on Linux):
java -cp library1.jar:library2.jar:mainapp.jar <fully qualified name of main class>
You need to set the CLASSPATH .place all the 3 jars in a folder , name it as lib
See below to set classpath
set CLASSPATH=%CLASSPATH%:lib;

Categories