How to provide classpath to Executable JAR file in mac terminal - java

I have an executable jar file i.e. main class is bundled in the jar with manifest entry.
I was able to run the jar file with the following command:
java -jar myApp.jar
Which is able to pick my main class.
Because of some libraries missing in the executable jar file, I am getting ClassNotFoundException.
So, to fix the class not found errors, I tried to pass few more jar files as classpath to the java command as below:
java -classpath /Users/lib/httpclient.jar:/Users/lib/commons-logging.jar -jar myApp.jar
But my main class is not able to find the jar files specified in -classpath argument.
Added print statement in my main class as below:
System.out.println("Classpath: " + System.getProperty("java.class.path"));
This print statement is printing the classpath as below
Classpath: myApp.jar
Why I am not able to see the other jars(httpclient.jar & commons-logging.jar) in classpath? How to add those jar files to classpath for executable jar file in mac terminal?

Related

java -cp option seems not to work in Java 14 (Preview Enabled) [duplicate]

I've compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled lib. I can't seem to get a straight answer on how to execute my JAR file while specifying it should use the lib/* as the classpath.
I've tried:
]$ java -jar -cp .:lib/* MyJar.jar
]$ java -cp .:lib/* -jar MyJar.jar
]$ java -cp .:lib/* com.somepackage.subpackage.Main
etc...
Each gives an error saying:
Error: Could not find or load main class ....
or gives the NoClassDefFoundError indicating the libraries are not being found.
I even tried remaking the JAR file and included the lib directory and contents, but still no dice...
How can I execute a JAR file from the command line and specify the classpath to use?
When you specify -jar then the -cp parameter will be ignored.
From the documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)
You have two options:
include all jar files from the lib directory into the manifest (you can use relative paths there)
Specify everything (including your jar) on the commandline using -cp:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Run a jar file and specify a class path like this:
java -cp <jar_name.jar:libs/*> com.test.App
jar_name.jar is the full name of the JAR you want to execute
libs/* is a path to your dependency JARs
com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method
The jar and dependent jar should have execute permissions.
You can do these in unix shell:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
You can do these in windows powershell:
java -cp "MyJar.jar;lib\*" com.somepackage.subpackage.Main
Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:
Main-Class: com.test.App
Class-Path: lib/one.jar lib/two.jar
Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.
Then execute it with
java -jar MyJar.jar
You can do a Runtime.getRuntime.exec(command) to relaunch the jar including classpath with args.

How can I execute a runnable jar with external classpath?

I'm trying to run a Runnable Jar in JAVA with an external jars folder as its classpath on Linux.
java -cp "/path/to/jars/*" -jar app.jar
But I'm receiving NoClassFound exception for a class that in the jars folder. What am I doing wrong?
Use full jar name in classpath. Like
java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
if you have more than one jar, then append them to the classpath with 'OS' separator.
java -classpath /java/MyClasses:/java/OtherClasses ...

Running JAR file from terminal (Runtime.getRuntime().exec) with both native and .jar dependencies

I am having trouble running a JAR file from terminal which has both native and .jar dependencies. Okay, my goal isn't to run it from the terminal, but to run it as a separate process with Java's Runtime.getRuntime().exec function, but if I can't run it from the terminal, then I also can't run it via. The JAR file I am trying to run depends on a number of other jar files as well as a number of .so libraries. I'm trying to put put all the .jar dependencies and .so dependencies in their own folders and then run the jar file with:
java -cp /home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/* -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/* -jar /home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar
Where "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/" contains all the JAR files and "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/" contains all the .so files and the main JAR file to run is "/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar", but I keep getting this error:
Error: Could not find or load main class
.home.johnmichaelreed.Desktop.Dropbox.Libjitsi_linux_64.some-compressed-jar-file.jar
Where some-compressed-jar-file.jar is one of the .jar files that my application is supposed to use.
Here's my Java JAR dependencies folder:
And here's my native libraries dependencies folder:
UPDATE:
Okay, this is the solution:
java -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64 -cp '/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar:/home/j‌​ohnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*' Main
With attempt at command line args:
java -Djava.library.path=/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64 -cp '/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar:/home/j‌​ohnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*' Main "arg"
You can't use -jar and -cp at the same time.
What you can do, is adding your jar to the classpath and then specify your Main class to run. You can also specify the jar dependencies within the manifest of your jar.
Please have a look here for more details.
Assuming your Main class is in called Main and in the package foo.bar, then a possible call may look like:
java -cp "/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/*;/home/johnmichaelreed/NetBeansProjects/SendReceive/dist/SendReceive.jar" -Djava.library.path="/home/johnmichaelreed/Desktop/Dropbox/Libjitsi_linux_64/lib/native/linux-64/*" foo.bar.Main

Run a JAR file from the command line and specify classpath

I've compiled a JAR file and specified the Main-Class in the manifest (I used the Eclipse Export function). My dependencies are all in a directory labeled lib. I can't seem to get a straight answer on how to execute my JAR file while specifying it should use the lib/* as the classpath.
I've tried:
]$ java -jar -cp .:lib/* MyJar.jar
]$ java -cp .:lib/* -jar MyJar.jar
]$ java -cp .:lib/* com.somepackage.subpackage.Main
etc...
Each gives an error saying:
Error: Could not find or load main class ....
or gives the NoClassDefFoundError indicating the libraries are not being found.
I even tried remaking the JAR file and included the lib directory and contents, but still no dice...
How can I execute a JAR file from the command line and specify the classpath to use?
When you specify -jar then the -cp parameter will be ignored.
From the documentation:
When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.
You also cannot "include" needed jar files into another jar file (you would need to extract their contents and put the .class files into your jar file)
You have two options:
include all jar files from the lib directory into the manifest (you can use relative paths there)
Specify everything (including your jar) on the commandline using -cp:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
Run a jar file and specify a class path like this:
java -cp <jar_name.jar:libs/*> com.test.App
jar_name.jar is the full name of the JAR you want to execute
libs/* is a path to your dependency JARs
com.test.App is the fully qualified name of the class from the JAR that has the main(String[]) method
The jar and dependent jar should have execute permissions.
You can do these in unix shell:
java -cp MyJar.jar:lib/* com.somepackage.subpackage.Main
You can do these in windows powershell:
java -cp "MyJar.jar;lib\*" com.somepackage.subpackage.Main
Alternatively, use the manifest to specify the class-path and main-class if you like, so then you don't need to use -cp or specify the main class. In your case it would contain lines like this:
Main-Class: com.test.App
Class-Path: lib/one.jar lib/two.jar
Unfortunately you need to spell out each jar in the manifest (not a biggie as you only do once, and you can use a script to build the file or use a build tool like ANT or Maven or Gradle). And the reference has to be a relative or absolute directory to where you run the java -jar MyJar.jar.
Then execute it with
java -jar MyJar.jar
You can do a Runtime.getRuntime.exec(command) to relaunch the jar including classpath with args.

Creating a exc jar file in eclipse ?

so I created a jar file, clicked on export and etc
but when I double click it doesnt do anything
I dont see my classes in the folder
all i see is
junit
org.hamcrest.core_1.1.0.v2009050107100…
what am i doing wrong ?
If your jar is a valid executable
then this should work
java -jar your.jar
Jar files don't aren't always run by double-clicking.
In order to run the jar file, run the command java -jar yourjarfile.jar while in the same directory as the jar file. This assumes that your PATH system variable is set properly.
If you receive an error such as "Failed to load Main-Class manifest attribute from yourjarfile.jar" it means that there is no main class defined in your jar file, and the java interpreter doesn't know where to start.
You can make sure that a main class is specified in Eclipse by exporting as a "Runnable JAR file" and making sure you select a launch configuration that you use to run your program.
In order to list the contents of the jar file, run the command jar tvf yourjarfile.jar while in the same directory as the jar file.
In order to extract the contents of the jar file, run the command jar xvf yourjarfile.jar while in the same directory as the jar file.

Categories