109

I was writing a simple program using a Java application (not application that has projects, but application within a project; .java) that has a single frame. Both of the files are .java so I can't write a manifest needed by the JAR.

The MyApp.java starts like a class with package, imports then public class MyApp and has a main function, but it's still .java file! I'm writing it in JDeveloper 11g if it helps.

Any ideas how to make a JAR from these files?

informatik01
  • 16,038
  • 10
  • 74
  • 104
user1303007
  • 1,091
  • 2
  • 8
  • 3
  • 2
    I think this can be accomplish much easier by using packaging tool such as ANT or Maven – Churk Mar 30 '12 at 10:33
  • Agree with Chuck. Take a look at ANT. IDEs are great for developing, but you need to understand how things works. java files are compiled via javac to .class files, after this, .class files can be packaged into a jar with the jar command. (Ant has tasks for doing this, even jdeveloper has some wizard for doing this). – BigMike Mar 30 '12 at 10:35
  • JDeveloper itself should have the capability to create jar files. A quick google search comes up with a howto here: http://tompeez.wordpress.com/2011/06/01/creating-a-jar-file-in-jdeveloper/ – stolzem Mar 30 '12 at 10:36
  • 1
    Can you clarify your question? – Romain Hippeau Mar 30 '12 at 10:38
  • Go to source dir and run command:- javac *.java && jar cvf JarName.jar *.class – garish Jul 31 '20 at 05:10

8 Answers8

133

Open a command prompt.

Go to the directory where you have your .java files

Create a directory build

Run java compilation from the command line

javac -d ./build *.java

if there are no errors, in the build directory you should have your class tree

move to the build directory and do a

jar cvf YourJar.jar *

For adding manifest check jar command line switches

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
BigMike
  • 6,683
  • 1
  • 23
  • 24
  • I've tried compiling from the command line, but I get an error: Frame1.java:23: package oracle.jdeveloper.layout does not exist How do I solve that? Sorry for the newbie questions... – user1303007 Mar 30 '12 at 13:55
  • you have to build the classpath referring all the libraries you're referencing in your classes. Check with your jdeveloper installation, there should be some ant build example (build.xml file) and at least a couple of good tutorials on how to use jDeveloper with ANT on Oracle's site. – BigMike Mar 30 '12 at 13:58
  • @user1303007 take a look here http://www.oracle.com/technetwork/articles/adf/part4-098813.html – BigMike Mar 30 '12 at 14:09
  • 1
    I've added an option to my jar command to automatically add the Main-Class to manifest... `jar cfe Main.jar Main *` (I've got confused though, I've had to put the Main-Class name after the output file name.) –  Mar 09 '17 at 11:55
  • I'd like to point out: `You have to move to build directory`. If you try `jar cvf YourJar.jar ./build/*`, you will have trouble running your jar file later. – DarkTrick Apr 20 '21 at 06:48
  • @DarkTrick precisely, similar issues you would get if you forget the -d switch on javac. – BigMike Apr 25 '21 at 20:48
42

Simply with command line:

javac MyApp.java
jar -cf myJar.jar MyApp.class

Sure IDEs avoid using command line terminal

Balu mallisetty
  • 603
  • 9
  • 19
ab_dev86
  • 1,952
  • 16
  • 21
21

Ok this is the solution I would have liked to find, instead here I write it:

First create the directory structure corresponding to the package defined for the .java file, if it is my.super.application create the directory my and inside it super and inside it the .java file App.java

then from command line:

javac -cp /path/to/lib1.jar:/path/to/lib2.jar path/to/my/super/App.java

Notice the above will include multiple libraries, if under windows use "," to separate multiple files otherwise under GNU/Linux use ":" To create a jar file

jar -cvfe App.jar App my/app/

the above will create the application with its corresponding Manifest indicating the App as the main class.

Including the required libraries inside the jar file is not possible using java or jar command line parameters.

You can instead:

  1. manually extract libraries to the root folder of the jar file
  2. use an IDE such as Netbeans and insert a rule inside post-jar section of nbproject/build-impl.xml to extract the libraries inside the jar. See below.
<target name="-post-jar">
    <!-- Empty placeholder for easier customization. -->
    <!-- You can override this target in the ../build.xml file. -->
    <jar jarfile="${dist.jar}" update="true">
        <zipfileset src="${dist.jar}"                           includes="**/*.class"/>
        <zipfileset src="${file.reference.iText-1.0.8.jar}"     includes="**/*"/>
        <zipfileset src="${file.reference.itextpdf-3.2.1.jar}"  includes="**/*"/>
    </jar>
</target>

the file.reference names are found inside project.properties file after you added the libraries to the Netbeans IDE.

StackzOfZtuff
  • 2,534
  • 1
  • 28
  • 25
sarah.ferguson
  • 3,167
  • 2
  • 23
  • 31
11

Often you will want to specify a manifest, like so:

jar -cvfm myJar.jar myManifest.txt myApp.class

Which reads: "create verbose jarFilename manifestFilename", followed by the files you want to include. Verbose means print messages about what it's doing.

Note that the name of the manifest file you supply can be anything, as jar will automatically rename it and put it into the right directory within the jar file.

Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124
6

This can be done without terminal, directly from IDE. Netbeans, for example.

  1. Create a separate project with packages (Create Project - Java - Java Class Library).
  2. Put your .java classes there.
  3. Build this project.
  4. Go to your project folder and find build and dist folders there.
  5. Find .jar file in your dist folder.
  6. Get your other project and add this .jar file to project libraries.
  7. You can now reference classes from this library and its methods directly from code, if import is automatically done for you.
Zon
  • 18,610
  • 7
  • 91
  • 99
5

Here is another fancy way of doing this:

$ ls | grep .java | xargs -I {} javac {} ; jar -cf myJar.jar *.class

Which will grab all the .java files ( ls | grep .java ) from your current directory and compile them into .class (xargs -I {} javac {}) and then create the jar file from the previously compiled classes (jar -cf myJar.jar *.class).

4

Perhaps the most beginner-friendly way to compile a JAR from your Java code is to use an IDE (integrated development environment; essentially just user-friendly software for development) like Netbeans or Eclipse.

  • Install and set-up an IDE. Here is the latest version of Eclipse.
  • Create a project in your IDE and put your Java files inside of the project folder.
  • Select the project in the IDE and export the project as a JAR. Double check that the appropriate java files are selected when exporting.

You can always do this all very easily with the command line. Make sure that you are in the same directory as the files targeted before executing a command such as this:

javac YourApp.java
jar -cf YourJar.jar YourApp.class

...changing "YourApp" and "YourJar" to the proper names of your files, respectively.

Randall Arms
  • 407
  • 1
  • 5
  • 22
2

If you have one simple main class(let's say Count.class), and want a jar with a MANIFEST file that points to this class as the entry point, you can do this:

jar cfe app.jar Count Count.class 

Source: https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html