Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I add dependency for Java?

Tags:

java

jar

I'm very new to Java. I have a single class file which is used to do some processing.

That class file is depended on a jar. I'm very new to Java, where I'm passing the jar to my classpath while running the program:

javac -classpath jar MyProgram.java

Now I wish to bundle both the jar and MyProgram into separate jar with dependency resolved.

Are there any ways to do this in Java? Note that it my MyProgram java is only around 50 lines of code, so some simple solution will be good.

Thanks in advance.

like image 487
sriram Avatar asked Feb 18 '23 23:02

sriram


1 Answers

You cannot put the library JAR inside another JAR file - Java will not be able to load classes from embedded JAR files.

You could create a JAR file containing just the classes of your application, and have the library JAR alongside it. See Packaging Programs in JAR Files for details on how to do this exactly.

If you really want everything to be in a single JAR file, you could use a tool such as One-JAR to package it.

like image 154
Jesper Avatar answered Feb 27 '23 09:02

Jesper