Problem with java classpath when executing jar - java

I am trying to execute a java application, which is packaged as jar archive.
java -cp .\lib\json.jar -jar ".\myarchive.jar"
I get an error saying that the classes inside my json.jar archive cannot be found.
Exception in thread "main" java.lang.NoClassDefFoundError: json/serializers/JsonTypeResolversInstance
Caused by: java.lang.ClassNotFoundException: json.serializers.JsonTypeResolversInstance
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
The jar file contains the class, so i think this error should not happen. When executing the code using my IDE it runs without errors.
I have tried to fix this in many ways, without success.

Using the -cp and -jar options at the same time does not work. When you use the -jar option, then the -cp option is ignored.
There are two ways to run code in a JAR file in Java.
First way: Use the -cp option and specify the name of the class that contains the main method on the command line. For example:
java -cp .\lib\json.jar;.\myarchive.jar com.mypackage.MyMainClass
When you do it like this, you specify the classpath on the command line (using -cp). The classpath must contain all JAR files and/or directories that contain all the classes that the application needs. You must also specify the name of the class to run on the command line.
Also, when you do it like this, the manifest file that might be present in the JAR file is ignored.
Second way: Use the -jar option. For example:
java -jar .\myarchive.jar
When you do it like this, then Java will look at the manifest file in the JAR file. The classpath and the name of the class to run will be taken from the manifest file, and the -cp option on the command line will be ignored.
For details see the documentation of the java command.

Related

JAR file is not read even though added in class path

I have a shell script which runs a java class presnet in /opt/mydir/ directory. In this script I am adding the class file along with few other jar files to the java classpath.
Ex:
/usr/bin/java -cp "/opt/mydir"
When the script is run, the java .class file fails with
Exception in thread "main" java.lang.NoClassDefFoundError: com/internal/altsite
The com.inetrnal.altsite comes from an external jar, and is also present in the same classpath as the main class file.
If i add the name of the jar explicitly to the classpath, the shell script runs successfully without any error. Ex:
/usr/bin/java -cp "/opt/mydir:/opt/mydir/altsite.jar:"
The directory structor is as follows:
-/opt
---mydir/
-------runner.class,altsite.jar,jose4j.jar

Unable to compile using Picocli

I'm a dev student
I would love to use Picocli in my project, unfortunately I doesn't understand how to compile using Picocli
I trie to follow the instruction given here https://picocli.info/ or here https://picocli.info/quick-guide.html but the step to compile aren't detailed. I'm not using Gradle nor Maven but they aren't really listed as required.
This is how it tried to compile the Checksum example given in the picocli.info webpage :
jar cf checksum.jar Checksum.java ; jar cf picocli-4.6.1.jar CommandLine.java && echo "hello" > hello
Then I simply copy paste this gived command : https://picocli.info/#_running_the_application
java -cp "picocli-4.6.1.jar:checksum.jar" CheckSum --algorithm SHA-1 hello
And get the following result :
Error: Could not find or load main class CheckSum
Caused by: java.lang.ClassNotFoundException: CheckSum
I tried to compile everything myself and then add the .jar like this :
java CheckSum -jar picocli-4.6.1.jar
But then the error output looks like this:
Exception in thread "main" java.lang.NoClassDefFoundError: picocli/CommandLine
at CheckSum.main(Checksum.java:33)
Caused by: java.lang.ClassNotFoundException: picocli.CommandLine
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
Witch I don't understand since I added the dependency.
What am I missing ?
Thanks in advance
The problem is that the command jar cf checksum.jar Checksum.java only creates a jar file (jar files are very similar to zip files) that contains the Checksum.java source file.
What you want to do instead is compile the source code first. After that, we can put the resulting Checksum.class file (note the .class extension instead of the .java extension) in the checksum.jar. The Java SDK includes the javac tool that can be used to compile the source code. Detailed steps follow below.
First, open a terminal window and navigate to a directory that contains both the Checksum.java source file and the picocli-4.6.1.jar library.
Now, the command to compile (on Windows) is:
javac -cp .;picocli-4.6.1.jar Checksum.java
Linux uses : as path separator instead of ;, so on Linux, the command to compile is:
javac -cp .:picocli-4.6.1.jar Checksum.java
The -cp option allows you to specify the classpath, which should contain the directories and jar/zip files containing any other class files that your project uses/depends on. Since Checksum.java uses the picocli classes, we put the picocli jar in the classpath. Also add the current directory . to the classpath when the current directory contains any classes. I just add . habitually now.
Now, if you list the files in the current directory, you should see that a file Checksum.class has been created in this directory.
Our Checksum class has a main method, so we can now run the program with the java tool:
On Windows:
java -cp .;picocli-4.6.1.jar Checksum
On Linux:
java -cp .:picocli-4.6.1.jar Checksum
Note that when running the program with java you specify the class name Checksum, not the file name Checksum.class.
You can pass arguments to the Checksum program by passing them on the command line immediately following the class name:
java -cp .:picocli-4.6.1.jar Checksum --algorithm=SHA-1 /path/to/hello
When your project grows, you may want to keep the source code and the compiled class files in separate directories. The javac compile utility has a -d option where you can specify the destination for the compiled class files. For example:
javac -cp picocli-4.6.1.jar:otherlib.jar -d /destination/path /path/to/source/*.java
This should generate .class files for the specified source files in the specified destination directory (/destination/path in the example above).
When you have many class files, you may want to bundle them in a single jar file. You can use the jar command for that. I often use the options -v (verbose) -c (create) -f (jar file name) when creating a jar for the compiled class files. For example:
jar -cvf MyJar.jar /destination/path/*.class /destination/path2/*.class
Enjoy!

Classpath contains only the running file

I'm having issues trying to use the Connector/J JDBC driver in my code.
The code I have written uses the Class.forName("com.mysql.jdbc.Driver").newInstance() to load the class before DriverManager.getConnection() is used to load the driver.
This results in the ClassNotFoundException for com.mydql.jdbc.Driver. I have the binary JAR file for the mysql connector, mysql-connector-java-5.1.26-bin.jar.
My code is packaged into a JAR file by building in Netbeans.
To run the code I am using the following
java -classpath "/path/to/mysql-connector-java-5.1.26-bin.jar" -jar MyJarFile.jar
This gives the exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Adding System.out.println(System.getProperty("java.class.path")); to the beginning of the program prints only MyJarFile.jar.
I have tried adding the jar file to the CLASSPATH variable using export, and setting the last part of the -classpath flag to lib/*, but with the same results.
I tried running the program from a .class file, instead. It complained about being unable to find or load the main class. It would run only when both the wildcard was used in the classpath and the MyJarFile.jar was in that location. It would simply hang at the loading of the Driver, though.
Does anyone have any thoughts as to what is going on?
Try don't mix -cp and -jar options, this might work:
java -cp "mysql-connector-java-5.1.26-bin.jar:MyJarFile.jar" my.package.Main
for *nix or
java -cp "mysql-connector-java-5.1.26-bin.jar;MyJarFile.jar" my.package.Main
for windows
where my.package.Main is your main class.

How to run Java program in terminal with external library JAR

This should be simple but I have never done it before and didn't find any solution.
I am currently using Eclipse to code my program, which imports some external JAR library such as google data api library. I can use Eclipse to compile/build/run the program.
But now I want to run it in terminal, so where should I put those JAR files, and how to build and run the program?
Thanks!
You can do :
1) javac -cp /path/to/jar/file Myprogram.java
2) java -cp .:/path/to/jar/file Myprogram
So, lets suppose your current working directory in terminal is src/Report/
javac -cp src/external/myfile.jar Reporter.java
java -cp .:src/external/myfile.jar Reporter
Take a look here to setup Classpath
For compiling the java file having dependency on a jar
javac -cp path_of_the_jar/jarName.jar className.java
For executing the class file
java -cp .;path_of_the_jar/jarName.jar className
you can set your classpath in the in the environment variabl CLASSPATH.
in linux, you can add like
CLASSPATH=.:/full/path/to/the/Jars, for example ..........src/external
and just run in side ......src/Report/
Javac Reporter.java
java Reporter
Similarily, you can set it in windows environment variables.
for example, in Win7
Right click Start-->Computer
then Properties-->Advanced System Setting --> Advanced -->Environment Variables
in the user variables, click classPath, and Edit and add the full path of jars at the end.
voila
Suppose your jar application "myapp.jar" has the following code snippet written inside it.
import org.json.JSONObject;
public class Main {
public static void main(String[] args) {
// write your code here
System.out.println("Hello World!");
JSONObject jo = new JSONObject("{ \"abc\" : \"def\" }");
System.out.println(jo.toString());
}
}
It is using the external library json.jar from which we imported "org.json.JSONObject".
Running the following command will result in an error.
java -jar myapp.jar
Exception message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject at com.reve.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: org.json.JSONObject at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
We must include the json.jar file while running the jar file. We have to specify the class path of the file before building our myapp.jar file.
Inside META-INF/MANIFEST.MF file:
Manifest-Version: 1.0
Class-Path: lib/json.jar lib/example2.jar
Main-Class: com.reve.Main
Specify the external libraries separated by spaces under the Class-Path keyword. Then after building the project and the artifact, we can run the jar file by simply writing the same command we discussed above.

Help to launch java app on linux

I have such bash script start.sh:
export JAVA_HOME=/home/qds/bin/jdk1.6.0_22
export QDS_HOME=/home/qds
$JAVA_HOME/bin/java -classpath $QDS_HOME/lib/*:$QDS_HOME/lib/commons/* com.qds.Main $#
In the directory /home/qds/lib I have necessary libraries and my jar file, wich contains:
com\qds\config
com\qds\entities
com\qds\hibernate
com\qds\protocols
com\qds\util
com\qds\Main.class
but when I run ./start.sh, I have:
./start.sh
Exception in thread "main" java.lang.NoClassDefFoundError: com/qds/Main (wrong name: Main)
at java.lang.ClassLoader.defineClass1(Native Method)
You have to specify what your main class is going to be in one of several ways
specify the main class name as an argument to the java command (just add the main class to the end of the your java command)
Define it in your main jar's manifest
Java does not support wildcards in option -classpath. Try to remove * from $QDS_HOME/lib/* and from $QDS_HOME/lib/commons/*
BTW this is relevant for windows too.
Classpath should contain
1. directories
2. explicitly written jar files.
If you have many jar files you can create script that composes the command line option.
For example on linux
-classpath `ls -1|tr '\n' :`
composes classpath delimited with colon

Categories