Friendly King
Friendly King

Reputation: 2476

Having trouble setting the Java classpath from terminal

My directories are structured as follows: /User/JAVA/MyProject/

In the MyProject folder, I have my Main.java and Helper.java files

In the JAVA folder, I have two JAR files: jar1.jar and jar2.jar

I would like to set my classpath to these two JARs so I can use them in my Main.java and Helper.java files. Very simple.

To do this, I have tried the following (as well as numerous other variations) from my terminal:

user:MyProject$ javac -cp "/User/JAVA/jar1.jar:/User/JAVA/jar2.jar:." Main.java

EDIT: I am now using this command, as suggested, and receiving the same errors

user:MyProject$ javac -cp "/User/JAVA/jar1.jar:/User/JAVA/jar2.jar:." Helper.java Main.java

From what I've read, this should work. I have specified the absolute locations of the two JARs, I have separated them by a colon : (I am on OSX), and I have also included my current directory .. Then I have specified the file containing my main method with Main.java.

Unfortunately, I keep receiving a "Cannot find symbol" for the classes' methods in the JARs when I try to compile.

Furthermore, when I try System.out.println(System.getProperty("java.class.path"));, I receive the following output ., indicating my classpath is still set to my current directory.


My error is this:

/User/JAVA/MyProject/Helper.java:13 cannot find symbol
symbol: variable StdOut
location: class Helper

To note: When I opened my project up in Eclipse and added the classpath via the IDE, everything worked as expected and no error messages appeared during compilation. Nevertheless, I still would like to figure out how to do this from terminal and without the IDE.

What am I doing wrong, and how do I fix this?
Thanks.

Upvotes: 1

Views: 7234

Answers (2)

MockerTim
MockerTim

Reputation: 2483

You should use the semicolon paths separator (;), as discribed in Setting the classpath document. But you are using colon instead (:).

Try to use the following command (copy and paste it):

javac -cp /User/JAVA/jar1.jar;/User/JAVA/jar2.jar Helper.java Main.java

from your /User/JAVA/MyProject directory.

EDIT:

All above is for Windows.

The problem is in paths themselves.

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76898

Using -cp when compiling or running sets the classpath. That overrides (ignores) anything you would have put in the env variable CLASSPATH.

Since you didn't post the actual error you're receiving I'm going to guess the missing symbol is from your other class that Main is referencing. Add it to the command:

javac -cp "/User/JAVA/jar1.jar:/User/JAVA/jar2.jar:." Helper.java Main.java

Edit due to the OP posting the actual error: You have a an error in your class that makes it uncompilable. It has nothing to do with jar files or classpaths. You are attempting to use / reference a variable named StdOut that doesn't exist.

Upvotes: 4

Related Questions