How to add dependency for Java?

I am very new to Java. I have one class file that is used for processing.

This class file depends on jar . I am very new to Java, where I pass the jar to my class path when the program starts:

 javac -classpath jar MyProgram.java 

Now I want to link both jar and MyProgram into a separate jar with the allowed dependency.

Are there any ways to do this in Java? Note that this java MyProgram is only about 50 lines of code, so some simple solution would be a good one.

Thanks in advance.

+4
source share
3 answers

You cannot put the JAR library in another JAR file - Java cannot load classes from embedded JAR files.

You can create a JAR file containing only the classes of your application, and with it have a library JAR. See "Packaging Programs in JAR Files" for instructions on how to do this.

If you really want everything in one JAR file, you can use a tool like One-JAR to pack it.

+4
source

You should not link both the compiled code and the dependency in a separate bank. You should only link the compiled classes in the bank, and when you start the program, you put both your bank and the dependent one in the class path.

Use the jar command to create the jar file, and then use the following command to start your program:

 java -classpath dependecy.jar;yourjar.jar MyProgram 
+2
source

You can just use this

 java -cp your-jar-file.jar yourclass 

For more information about Class-Path, go to PATH and CLASSPATH .

0
source

Source: https://habr.com/ru/post/1437749/


All Articles