How to Use .Jar Files in Netbeans

How to add a JAR in NetBeans

You want to add libraries to your project and in doing so you have two options as you yourself identified:

Compile-time libraries are libraries which is needed to compile your application. They are not included when your application is assembled (e.g., into a war-file). Libraries of this kind must be provided by the container running your project.

This is useful in situation when
you want to vary API and implementation, or when the library is supplied by the container (which is typically the case with javax.servlet which is required to compile but provided by the application server, e.g., Apache Tomcat).

Run-time libraries are libraries which is needed both for compilation and when running your project. This is probably what you want in most cases. If for instance your project is packaged into a war/ear, then these libraries will be included in the package.

As for the other alernatives you have either global libraries using Library Manageror jdk libraries. The latter is simply your regular java libraries, while the former is just a way for your to store a set of libraries under a common name. For all your future projects, instead of manually assigning the libraries you can simply select to import them from your Library Manager.

Referencing .jar files in NetBeans

In order to use .jar files in NetBeans, you can either add the jar file to your global libraries or your local libraries. It sounds like you tried doing it locally without much success. I recommend adding a library such that you can just pick from your list of libraries without needing to reload it. I use NetBeans 8.1 so it may be just a bit different for 6.8.

What you need to do is go to Tools->Library

Then you need to click on New Library at the bottom left of the dialogue box.

AddLibrary

Name it and hit 'Ok'. Then, you'll add a new jar file by specifying your jar path. Hit 'Ok' again to save everything.

In order to use your new library, you'll need to then click on your Libraries folder in your project and add a new library. Select from the list of libraries the library you just created.

SelectLibrary

Finally, you import your individual files in your library using the #import keyword as per usual. If you want to import everything, you'd type something akin to

import MyNewLibrary.*;

To load a library the way you tried to do it, you'd right click the libraries folder and click "Add JAR/folder" instead of "Add Library". Then you'd import everything.

If you still for some reason cannot get your library to work, then you probably aren't importing using the right name. Or, you might be trying to load a static library without using the static keyword.

How can I include external jar on my Netbeans project

If you copy your jars into the source code directory, they will be in your final jar. Nevetheless, I am not sure if this will work 100% of the time.

There is a great post at java-forum that states the following:

Except for a select few circumstances, what works best for me is to
simply merge the files manually. A .jar is basically a .zip with
organized contents, and you can open them in almost any .zip capable
archive program (I just use gnome's standard archiver, File Roller,
and it works great). Backup your jar file and open it in the archiver
of your choice, and do the same for each library jar in the library
directory. Drag and drop the working folders (IE, everything EXCEPT
the META-INF Directory) from each library into your jar's root path
(alongside your META-INF and your app's root package). Now drag the
META-INF/MANIFEST.MF file from your jar to your Desktop or any other
folder. Open it, and erase the Class-Path and X-COMMENT lines. Don't
forget to leave a blank newline at the end of the file! Save the new
manifest file and drag it back to your jar's META-INF directory,
overwriting the old one. Test the jar.

Running .JAR file from java code (netbeans)

Use this variant of .exec where you specify working folder as the third argument. (In your examples, you always only use one argument.)

exec("javaw -jar "C:\\Software\\program.jar", null, "C:\\Software");


Related Topics



Leave a reply



Submit