Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple jar files in classpath in linux

Tags:

Okay, I'm very new to linux and command line, and fairly new to java. I got an internship building a java program. I finally got it done on my machine (windows) and now I have to migrate it to a linux machine to test and then have it run as an executable. I have done much reading and researching on linux and understanding classpaths but it is still all very hard to fully comprehend. It's just not clicking for me yet. Can anyone explain the purpose of classpath in a simplified way using examples? One of the most confusing aspects to me is actually defining the physical path to the jar. Do I start all the way from usr or do I only need to begin from the jvm folder? If it matters, my java program is not located in the jvm folder. Can anyone shed some light for me?

EDIT: thank you guys very much for your help, I can't say that I'm fully in the clear but my understanding of my situation is a lot better.

like image 792
exit_1 Avatar asked Jul 12 '12 19:07

exit_1


People also ask

How do I find the classpath of a jar in Linux?

Another way of viewing the classpath is to run this Java code: String classpath = System. getProperty("java. class.

How do I find the classpath of a JAR file?

To check our CLASSPATH on Windows we can open a command prompt and type echo %CLASSPATH%. To check it on a Mac you need to open a terminal and type echo $CLASSPATH.

How do I add a JAR file to a Classpath?

Two methods for adding a JAR file to a classpath are available: method-add and method-add-delete. Right-click on the name of your project in Step 1. The second step is to select the build path on the Build Path tab. Choose the configure build path option at Step 3.

What is the use of Classpath in Linux?

In this case, CLASSPATH shell variable contains the list of Jar file which is required by the application. One of the best advantages of using classpath command-line option is that it allows us to use every application to have its own set of JAR classpath.

What is the classpath in Java 6?

Java - classpath /classes: /lib/* In Java 6 wildcard which includes all JAR, it will not search for JARs in a subdirectory. Wildcard is included in all JAR is not honored in the case when we are running Java program with JAR file and having Class-Path attribute in the manifest file.

How do I add a JAR file to my project?

1 Step 1: Right-Click on your project name 2 Step 2: Click on Build Path 3 Step 3: Click on configure build path 4 Step 4: Click on libraries and click on “Add External JARs” 5 Step 5: Select the jar file from the folder where you have saved your jar file 6 Step 6: Click on Apply and Ok. More ...


2 Answers

Say you have multiple jar files a.jar,b.jar and c.jar. To add them to classpath while compiling you need to do

$javac -cp .:a.jar:b.jar:c.jar HelloWorld.java

To run do

$java -cp .:a.jar:b.jar:c.jar HelloWorld
like image 67
noPE Avatar answered Oct 19 '22 05:10

noPE


You use the -classpath argument. You can use either a relative or absolute path. What that means is you can use a path relative to your current directory, OR you can use an absolute path that starts at the root /.

Example:

bash$ java -classpath path/to/jar/file MyMainClass

In this example the main function is located in MyMainClass and would be included somewhere in the jar file.

For compiling you need to use javac

Example:

bash$ javac -classpath path/to/jar/file MyMainClass.java

You can also specify the classpath via the environment variable, follow this example:

bash$ export CLASSPATH="path/to/jar/file:path/tojar/file2"
bash$ javac MyMainClass.java

For any normally complex java project you should look for the ant script named build.xml

like image 43
Petriborg Avatar answered Oct 19 '22 06:10

Petriborg