0

How to include all jar files in Java? I'm using command prompt.

My jar files is stored on C:\test\java

jarfiles is:

commons-io.jar, commons-lang.jar, opencsv.jar

And my java program is also stored on C:\test\java

I've searched on google but it seems I cannot find the proper way of using classpath.

I'm currently on C:\test\java and using this command

javac -cp ".;commons-io.jar;commons-lang.jar;opencsv.jar;" JavaTest.java

And it's compiling successfully but when I run my java program with this line

Java JavaTest

I'm having a error with the opencsvWriter.

Exception in thread "main" java.lang.NoclassDefFoundError: au/com/bytecode/opencsv/CSVWriter

I cannot determine if classpath is wrong or on the writer is wrong.

Thanks in advance!

tuturyokgaming
  • 249
  • 1
  • 8
  • 21

1 Answers1

0

The javac command will not link togther libraries into a single executable file unlike how the c/c++ linker can with object files. Instead, you have to specify the same classes in your classpath when you go to run the compiled java file:

java -cp ".;commons-io.jar;commons-lang.jar;opencsv.jar;" JavaTest

You could optionally bundle all of the files into a single jar, and then specify your classpath inside the manifest file located in the newly created jar.

Some additional reading on that here:

Reference jars inside a jar

Community
  • 1
  • 1
Harvtronix
  • 854
  • 6
  • 16