How to combine the library with my bank?

Ok, so I wrote a program that uses a third-party open source library, and I want to pack it using my program in one bank. I am using netbeans 6.8, and all I tried, java always spun error:

java.lang.NoClassDefFoundError: libraryname; 

off topic: also I would like to know how to make an executable jar (exe) through netbeans, if possible. (ive saw programs that were written in java but were .exe)

EDIT has discovered an eclipse plugin called FatJar that can do what I want, but I cannot find something similar for netbeans, is there such a thing?

+4
source share
8 answers

I will start with a mandatory disclaimer: Java-executable JAR files do not work this way. The executable JAR has a main class defined in the MANIFEST.MF JAR file, and MANIFEST also allows you to define the class path to include the libraries that are required for the code in the executable JAR. A class path definition in MANIFEST should list each JAR or folder to host the class path, relative paths relate to the location of the executable JAR β€” not to paths containing the executable JAR inside . Executable JARs are launched with the -jar argument to the java executable, and both the java flag β€œ-cp” and the CLASSPATH environment variable are ignored . As to why executable JAR modules were designed in this way, you should be aware of the primary drawback of loading classes from JARs contained in the JAR, although the rest of this answer will focus on this.

NOTE. I lost the original forum topic on the sun, which fully explained this, but essentially because the entries in the top-level JAR can be read in any way, but the entire built-in JAR must be read before any entries can be read, therefore that a top-level JAR could compress its records.

I have used One-Jar in the past, but the structure of the final final jar may not be what you expect. In fact, the One-Jar classes are the only classes other than JARd in the last bank; all other code (your code and any dependent library code) is included in the resulting JAR file as a JAR. Your JARed application is a regular JAR file named "main.jar" in the last main JAR folder. Any libraries your code needs are placed as JAR files in the last JAR folder "lib". And last but not least, the last JAR file MANIFEST.MF tells One-Jar what your main class is. Execution is a dead simple "java -jar final.jar [args that your application uses]". I don’t know how to take the next step to convert to OS-native EXE regarding your custom question, but it is probably best to use a different packaging mechanism than One-Jar. I'm not sure how to do this with NetBeans, my advice is to use the build tool to pack the final jar. Fortunately, One-Jar provides instructions for creating the final jar using Ant, and this should be easily integrated into NetBeans.

I believe the Eclipse FatJar plugin creates an executable JAR with one Jar, so if this plugin seems to do what you want, then One-Jar is the way to do it. Personally, I used the Maven build.

There is a caveat β€” any signed libraries that require (or desire) to use the Java JAR check may not work that way β€” an implementation of the Java Cryptographic Extension (JCE) such as BouncyCastle is a notable example. I think the reason is because signature verification works against the final JAR, not from a signed library. Fortunately, One-Jar allows the end user to add additional libraries to the class path, which is explicitly prohibited when running the executable JAR; in order to get around this, you might be better off delivering problematic JARs with a final JAR and OS launching dependent on an OS script (.bat, .sh, etc.).

+16
source

I understand that this is not exactly what you want, but I will describe the usual method of distributing a single application. If it meets your needs, you will find that it is better supported by tools and more understandable to users because it complies with established conventions.

Put your code in the jar (I will call it app.jar ) along with the META-INF/MANIFEST.MF file with the following elements:

 Main-Class: com.y.app.AppMain Class-path: third-party.jar blort.jar foo.jar 

Then you can drop all the banks in the directory and launch AppMain as follows:

 java -jar app.jar 

If you want, you can put third-party libraries in one directory, for example lib , and refer to them in the Class-path attribute, using the path relative to the main bank: lib/third-party.jar This helps to keep the distribution order.

+4
source

My general answer to your off-topic question is a rather long article: Converting Java to EXE - Why, When, When Not, and How . It has many links to free and commercial tools, but I have never seen a Netbeans plugin with such functionality, sorry.

+4
source

To include another jar in the jar, you can find jarjar .

In executable jars, there is simply a class defined as "Basic", if I'm not mistaken. This may be helpful .

+3
source

It was a great guide and really did what I wanted without cheating in all of these third-party libraries.

http://java.sun.com/developer/technicalArticles/java_warehouse/single_jar/

+2
source

If there is no need to repack third-party banks in your last big jar, then this should be the easiest method.

+1
source

If there are no licencing issues , then the most preferred way is to untie the actual jar and rewrite it with your class files into it, into a new jar.

You can just use jar cmd for this, no matter!

0
source

if you use MAVEN, use the maven-shade-plugin plugin. It will compile the jar with all the dependencies (third-party, etc.).

0
source

All Articles