4

I downloaded a sample code written in java that has multiple jar files and java files. I am not a Java programmer so I am having a hard time compiling the code. Here's my attempt:

javac -classpath lib/*.jar src/*.java

However this is what I get:

javac: invalid flag: lib/dom4j-1.6.1.jar
Usage: javac <options> <source files>
use -help for a list of possible options

What's wrong with my approach and how can I compile the code? ALl the jar files are located in the lib folder, and the java files in the src folder.

3
  • I suggest you use an IDE to do this for you. An IDE is design to make editing, compiling, running, and debugging easier etc. Commented May 18, 2015 at 22:42
  • Which IDE would you recommend? Commented May 18, 2015 at 22:45
  • You could try Netbeans, Eclipse or IntelliJ CE. They are all free and different benefits. Commented May 18, 2015 at 22:46

2 Answers 2

14

You need to stop the shell from globbing the wild-card in lib/*.jar by escaping it.

Also, you need to remove the .jar suffix ... because that's how classpath wildcards work; see Oracle's "Setting the classpath" document.

So ...

javac -classpath lib/\* src/*.java

Using an IDE is another option. However, if all you want to do is compile and run, then downloading and installing and learning to use an IDE is overkill (IMO). And the flipside is that it is good for an IDE-using Java programmer to also understand how to compile and run from the shell prompt ...

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Do you have to call the lib files to run the compiled program to? or should I just run java main.java?
"Do you have to call the lib files to run the compiled program to?" - You need to include them on the runtime classpath ... along with the Java compiler output directory. This is all described in the manual entries, and I would encourage you to read them.
3

old post, but thought below details help, you can specify jar files by separating by ; in windows and : in unix

Eg: (windows)

javac -cp first.jar;second.jar;third.jar YourClass.java

(unix)

javac -cp first.jar:second.jar:third.jar YourClass.java

Source: https://gullele.com/pass-all-the-jars-in-classpath-when-compiling-java/

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.