views:

108

answers:

4

How can i make a jar file from both the command line and Netbeans 6.7?

+7  A: 

Using the JAR command:

jar cf jar-file input-file(s)

Using Maven:

<packaging>jar</packaging>

Using Ant:

 <jar destfile="${dist}/lib/app.jar" basedir="${build}/classes"/>
Taylor Leese
+1 for the ant entries.
Frank V
jar cf jar-file input-file(s)in the input-file we have to write the name of file right?like for examplejar cf jar-file Convert.java???
Sundhas
@Sundhas - you want to put `.class` files in your JAR, not source files.
danben
@Sundhas - Here is a concrete example "jar cf my.jar My.class"
Taylor Leese
Right get it now :)Thanks alot Danben and Taylor L.
Sundhas
I have one more question..How to execute that jar file via notepad? and whats the manifest folder all about? what it does?
Sundhas
To execute a jar file: java -jar app.jarYou will also need to have a MANIFEST.MF file in the META-INF directory that define the Main-class like this:Manifest-Version: 1.0Created-By: 1.6.0 (Sun Microsystems Inc.)Main-Class: MyPackage.MyClass
Taylor Leese
Take a look at this link: http://java.sun.com/docs/books/tutorial/deployment/jar/appman.html
Taylor Leese
+2  A: 

Command line:

jar cf jarfile [ -C dir ] inputfiles

Make sure you are jar-ing from the root of the directory matching your package hierarchy, rather than just the directory with the class files. The directory structure needs to match the hierarchy.

Also, if you want the JAR to be executable, you need to include a MANIFEST.MF containing a Main-class entry specifying which class should be used as the entry point (this class must define a public static void main(String[] args)).

Netbeans: here is a link to a tutorial.

danben
+6  A: 
jar cf jar-file input-files

http://java.sun.com/developer/Books/javaprogramming/JAR/basics/build.html

Yada
A: 

There are many ways (enough already answered here), but if you want a REALLY SIMPLE way, look at Maven. It just works: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

Bill K