1

I am trying to run a simple java class using Windows command line. I have set JAVA_HOME and added it to the System path variable.

I have multiple jars in different folders and I am trying to add all of them using classpath using the below command. However when "*" is used, only the first path is picked by classpath and it is ignoring the remaining paths though I am using the ";" character. It does not work if the path uses the "/" separator. Is it possible to specify paths to multiple folders that have jar files and compile from a command line

javac -verbose -classpath "C:\Program Files\lib\java\core\*; C:\Program Files\lib\java\core\locale\*; C:\Program Files\lib\java\modules\*; C:\Program Files\lib\java\modules\ext\*;" testClass.java

Ultimately I want to use this classpath setup in an ant script in netbeans project properties to simplify the setup.

4
  • If you're using that path in an ant script, couldn't you let the script pick up those paths (e.g. configure them via properties) and let it expand them to the actual jar paths? Commented Oct 22, 2019 at 15:32
  • 1
    can you double check your command line- the should be -classpath folder\* . That is, dont't forget the slash before * Commented Oct 22, 2019 at 15:52
  • Remove all spaces after each ;. Whitespace is not ignored in a classpath. (And put a directory separator before each asterisk, as Daniele said.) Commented Oct 22, 2019 at 16:52
  • yes the stackoverflow editor was not displaying the slash before the * if I use a single backslash \* in the path, I have updated the question to use \\* and to display it as expected Commented Oct 22, 2019 at 21:21

2 Answers 2

2

Here is the solution that finally worked as expected. When the '*' character is used in the classpath for my specific scenario, it skipped everything after the first path. Using the double quotes (") for each path separately and then using ";" as the delimiter is the solution.

Having any space before or after the semi-colon ";" will also not work

javac -verbose -classpath "C:\Program Files\lib\java\core\\*";"C:\Program Files\lib\java\core\locale\\*";"C:\Program Files\lib\java\modules\\*";"C:\Program Files\lib\java\modules\ext\\*" testClass.java
Sign up to request clarification or add additional context in comments.

Comments

1

If you have jars in different directories, you need to set class path to all of this directory.

Windows

java -cp ./folder1/*;./folder2/*;./folder3/* com.xyz.MainClass

Linux ( ; is changed to :)

java -cp ./folder1/*:./folder2/*:./folder3/* com.xyz.MainClass

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.