开发者

Working with Java JAR files from cmdline

开发者 https://www.devze.com 2023-04-08 18:08 出处:网络
Let us have a Java application, contained in files A.java and B.java with dependency on somejar.jar file. The questions are:

Let us have a Java application, contained in files A.java and B.java with dependency on somejar.jar file. The questions are:

  • how to create a self-runnable JAR file with all the dependencies and sources compiled within? The main condition is using the standard Java utilities only (given with JDK; e.g.: java, javac and jar) and NOT any of build tools like Maven, Ant or any other.

  • how to use external JAR files within my applicatio开发者_开发技巧n? For example, the algorithm is: if the 'otherjar.jar' is present near the main application JAR, we should call method Moo::method1 from that class, passing the new instance of Foo class to it. Moo and Foo should be present in 'otherjar.jar' file. Still, the 'config.xml' file should be there too.


From the link that @michael667 gave, you may be more interested in this section: Adding Classes to the JAR File's Classpath. In particular this note:

Note: The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols. To load classes in JAR files within a JAR file into the class path, you must write custom code to load those classes. For example, if MyJar.jar contains another JAR file called MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path.

It's not a possible thing to do with the standard java tools if you don't code your own classloader. There are tools out there, like One-jar which can provide you of such classloader.

Of course, you could always manually use the exploding-jar approach, but that doesn't seem to be what you really want.

You may also find the answers to this question useful: Classpath including JAR within a JAR


This should answer most of your questions: http://download.oracle.com/javase/tutorial/deployment/jar/basicsindex.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号