JAR file is not read even though added in class path - java

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

Related

Problem with java classpath when executing jar

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.

Unable to run Java main class though windows commandline

I have an external jar and I have written 2 classes named 'TestConnector.java' and 'APIExampleUsageTestData.java' and I have placed these 2 files under default package.
I have placed all the 3 artifacts -> externalConnector.jar, TestConnector.java and APIExampleUsageTestData.java into a folder named 'temp'.
I have opened commandline in the temp folder context and compiled the class files with below command and it executed without any errors:
javac -cp "externalConnector.jar" *.java
I can see that there are 2 class files present in the tem folder.
There is a main method in the APIExampleUsageTestData.class and I'm trying to call that class with the below command, but, is failing
When running the below command in 'C:\Users\AppData\Local\Temp\0.15429262750877082' folder context
java -cp "externalConnector.jar" APIExampleUsageTestData
I'm getting error Error: Could not find or load main class APIExampleUsageTestData
When I trying to run the command, without any class path entry to check what error it is throwing:
java APIExampleUsageTestData
It throws error: Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: com/external/connector/CustomException
It is a Windows Operating System. Could you please let me know how to run the program successfully?
You need both the current directory and the jar file to be on the classpath. So you want (Linux / Mac):
java -cp .:externalConnector.jar APIExampleUsageTestData
Or (Windows)
java -cp .;externalConnector.jar APIExampleUsageTestData

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!

executing java -jar via classpath vs in the jar file dir

After having used NetBeans to create a Java program call it Addition and then having successfully cleaned and built an Executable Jar File in a folder c:\Users\Ben\Doc\NetBeansProjects\Addition\dist
WHY is it that when executing, from command prompt,
c:\Users\Ben Java -Jar -cp "c:\Users\Ben\Doc\NetBeansProjects\Addition\dist" Addition.jar
it does NOT work (i get 'unable to access jarfile Addition.jar)
BUT if i use cd to change my current dir to c:\Users\Ben\Doc\NetBeansProjects\Addition\dist and THEN run 'java -jar Addition.jar' from there, the Addition program runs fine
The -classpath argument is ignored when you use the -jar option. See the documentation.
because java doesn't look in classpath to launch jar file for this command it needs file as input
so if you set the directory where your jar file is placed and try to execute java -jar command and expect it to pick up jar from that directory because it is in classpath it is not valid
you can give full path to jar like from any directory
java -jar c:\Users\Ben\Doc\NetBeansProjects\Addition\dist\Addition.jar

Error unable to access jarfile C:\Jar

My jar file P2.jar (which has a main method and manifest file) is located in C:\Jar Folder. I tried to run it from cmd like this:
C:SomeRandomFolder> java -jar C:\Jar Folder\P2.jar
I get the error:
unable to access jarfile C:\Jar
It works when I run it from the folder that the jar is in like this:
C:\Jar Folder> java -jar P2.jar
Why can't I use the 1st command? Why do I have to go to the directory of my jar file?
That's because of the space you have in the path name. On Windows, use quotes:
java -jar "C:\Jar Folder\P2.jar"
If you are in a terminal and the folder you are in is deleted out from underneath you and replaced with the same content, the jar file will be be visible with ls, but will not be available since your current directory was deleted and replaced with one that looks just like it.
I got that error too:
el#apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Unable to access jarfile CalculateStats.jar
To fix it. cd up a directory and cd back in, like this:
el#apollo:/myproject/build/jar$ cd ..
el#apollo:/myproject/build$ cd jar/
el#apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Program completed successfully

Categories