8

I have a java class which has almost 12 jar file dependencies and i am using ubuntu 12.10 . I need to know how to run this java application because every time i run it , it gives me errors as "symbols not found". I have all jar files in a folder called libs. and i have tried these commands but none of these gives me some succesful result.I have flights.java class in test directory and libs directory is inside test directory.Currently i am in test directory

javac -cp "/home/ubuntu/test/libs/*.jar" flights.java

javac -cp '/home/ubuntu/test/libs/*.jar' flights.java

user1708240
  • 315
  • 2
  • 6
  • 16

2 Answers2

14

if you have single class in your app called flights.java and all of your required jar are located at /home/ubuntu/test/libs/ then use this

javac -cp '.:/home/ubuntu/test/libs/*.jar' flights.java

and to run

java -cp '.:/home/ubuntu/test/libs/*.jar' flights

better to just pack dependency and app in to a single jar and make it launchable and runnable jar

jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    Can u please tell me how to go for using second option?? – user1708240 Apr 01 '14 at 19:44
  • you would need a build tool for that, maven is very good here http://stackoverflow.com/questions/16222748/building-a-fat-jar-using-maven – jmj Apr 01 '14 at 19:45
  • This command "javac -cp '.:/home/ubuntu/test/libs/*.jar' flights.java "is giving me same error as earlier . – user1708240 Apr 02 '14 at 12:55
  • You don't write *.jar at the end. Just use * after the folder name. That is, use "java -cp .:/home/ubuntu/test/libs/* flights" – Jaywalker Sep 28 '16 at 11:59
  • Problems frequently appear with compiling java files with different packages inside. Deleting them can **simplify** your **compiling** in **cmd** – Dmytro Melnychuk Mar 08 '17 at 20:16
5

12 jars is not a very large number. Why not just append all the jars on the classpath?

Alternatively, you can create another jar and specify all the jars in Class-Path variable in that jar's MANIFEST.MF and then add this single jar to your classpath.

EDIT:

Here is how I would do it. Create a MANIFEST.MF file with content similar to this:

Manifest-Version: 1.0    
Archiver-Version: whatever  
Created-By: whatever  
Built-By: author-name  
Build-Jdk: 1.6.0_34  
Class-Path: jar1.jar jar2.jar jar3.jar  

replace jar1.jar with the actual file names of the jar.

Then you can create a jar with command : jar cvf test.jar -m ./MANIFEST.MF .

Now when you are using it on classpath use it like java -jar xyz.jar class-name

parik
  • 2,313
  • 12
  • 39
  • 67
J.J
  • 633
  • 1
  • 6
  • 14
  • Can u please guide me step by step how to do a jar file containing all jar dependencies and then use this jar while giving javac command. Thanks in advance – user1708240 Apr 02 '14 at 13:03