creating jar file

I am new to Java world.

I have bunch of java files. How do I compile files and create app.jar file so that user can run an application something like

java -jar app.jar
learningunixAsked:
Who is Participating?
 
for_yanConnect With a Mentor Commented:

Compare - you are not dioing this way - look the have manifets file mentioned in the
jarring filre exoplicitly:


---
We want to execute the main method in the class MyClass in the package MyPackage when we run the JAR file.

We first create a text file named Manifest.txt with the following contents:

Main-Class: MyPackage.MyClass

Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
We then create a JAR file named MyJar.jar by entering the following command:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

This creates the JAR file with a manifest with the following contents:

Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: MyPackage.MyClass

When you run the JAR file with the following command, the main method of MyClass executes:

java -jar MyJar.jar
---
0
 
learningunixAuthor Commented:
I just found this

jar cf jar-file input-file(s)


what are input-file(s)  
are these class files?

How do I create class files from my bunch of .java files
0
 
evedderCommented:
yup your input files are your classes, you have to compile your java files to get your classes, something like

javac YourFile.java

results in YourFile.class
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
learningunixAuthor Commented:
Please pardon my stupid questions as I am new to Java.

There are 3-4 java files and only one file has
public static void main

Should I still compile all files individually?

Once I create jar file, how will it know that it has to start from file "A" which has
public static void main
0
 
for_yanCommented:

After you have all files compiled you'll start your java application withn the command

java A

where A.class is the one which has main() method
0
 
for_yanCommented:

If you type some different class

like

java B

and B does not have main() method
you'll get exception - main method not found or soimethin like that
0
 
for_yanCommented:


if you wanrt to create executabkle jar

then it will put manifest in that jar and that manifest fiulke
will know whauich class has main maethod
0
 
for_yanCommented:
So JVM (java command) will lok inside the jar file and
find there manifest.mf file
and this manifest.mf file has a line which tells JVM
which class has a main method
0
 
evedderConnect With a Mentor Commented:
you can compile them individually or all together if they are in the same folder (or package in java language). To compile them all just
javac *.java

Open in new window


once you have all your classes you have to make a folder called META-INF, inside that folder you create a file called MANIFEST.MF inside you should have a line like MAIN-CLASS: MyClass.

Here is more info in the manifest file http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
0
 
for_yanCommented:

You usually create executable jar with jar command specifying
that you ant it to be executable and then jar command will create
thei mainfestmf file for you (as far as I rememeber).

Howvere much more often folks use IDE for that purpose - Eclipse or some other folks do i
0
 
learningunixAuthor Commented:
I have for .java files under  (only one has public static void main)
C:\DELETE\com\my\project

cd to C:\DELETE\com\my\project

All files has starting line
package com.my.project;

I was able to compile 3 files (which did not have main()) using javac <file>

However if I do javac <file with main>  it fails with

Driver.java:31: cannot find symbol
symbol  : class A

I also tried giving -cp and -classpath option both
0
 
learningunixAuthor Commented:
somehow
javac *.java worked
0
 
for_yanCommented:
you should go to C:\DELETE folder and from there
compuile

javac com/mt/project/A.java
0
 
for_yanCommented:
well, it may work javac *.java


but the normal way to compile them woiuld be
from the upper folder with this path
as I wrote above
0
 
learningunixAuthor Commented:
I cd to C:\DELETE\com\my\project

javac *.java   and wab able to comile all files

in the same directory, I ran
jar cf  TEST.jar  *.class

In the same directory, I ran   java -jar TEST.jar
Failed to load Main-Class manifest attribute from
TEST.jar
0
 
for_yanCommented:
If they don't have internal dependecy it will work form any place
but if you refer to some another class form the same packeage inside the code
it will be trying to locate it in this path com/my/project starting form the curret dierctory where you are compiling
so it woul not always worjk with dependency unless you start friom the yupper location as I specified
0
 
for_yanCommented:
If you want to do it manually - then follow the link as eveede recommended
0
 
for_yanCommented:

I probably was wring that it will do manifest.mf automatically for you - you need to type it yourself and place it
in the folder as specifyed in the link above
0
 
learningunixAuthor Commented:
I cd to C:\DELETE\

javac C:\DELETE\com\my\project*.java   and was able to comile all files

in the same directory, I ran
jar cf  TEST.jar  C:\DELETE\com\my\project\*.class

In the same directory, I ran   java -jar TEST.jar  and still same error
Failed to load Main-Class manifest attribute from
TEST.jar
0
 
for_yanCommented:
If you would do it in IDE then you just select option to create executable jar and
it will cretae this file for you
0
 
learningunixAuthor Commented:
Can I do it manually instead of installing eclipse for java
0
 
for_yanCommented:
Yes, follow the link above - if you have JDK 6 - read the last paragraph - it is actually automatoically
0
 
for_yanCommented:


The 'e' flag (for 'entrypoint'), introduced in JDK 6, creates or overrides the manifest's Main-Class attribute. It can be used while creating or updating a jar file. Use it to specify the application entry point without editing or creating the manifest file.
For example, this command creates app.jar where the Main-Class attribute value in the manifest is set to MyApp:

jar cfe app.jar MyApp MyApp.class

You can directly invoke this application by running the following command:

java -jar app.jar
0
 
for_yanCommented:
So from your C:\DELETE folder I think you should say:

jar cfe app.jar A com/my/project/*.class

if your main method is in class A

0
 
learningunixAuthor Commented:
I have java 1_5 version and the option e is invalid. let me try with the link you gave above
0
 
for_yanCommented:
Yes, then you need to create the manifest.mf manually and then mention it explicitly in the Jar
Please, always rememeber that Java is everywhere case sensitive
even if your files are in Windows - for Java they should have consistent case
0
 
learningunixAuthor Commented:
this time i manually created
C:\DELETE\META-INF

and

C:\DELETE\META-INF\MAINFEST>MF
Manifest-Version: 1.0
Created-By: 1.6.0_24 (Sun Microsystems Inc.)
Main-Class: com.my.project.MainClass

then I run
jar cf TEST.jar com\my\project\*.class

still I am getting:

 java -jar TEST.jar  and still same error
Failed to load Main-Class manifest attribute from
TEST.jar
0
 
learningunixAuthor Commented:
there was just a typo. filename was   MAINFEST.MF
0
 
learningunixAuthor Commented:
I copied the jar file to some other directory  and then ran
jar xf TEST.jar

The content of META_INF\MAINFEST.MF  was different
It did not have Main-Class entry

which means it is reading the content in the previous directory

could it be due to java 1.5 version?
0
 
learningunixAuthor Commented:
finally I got it. Thanks a lot for your help and patience
0
 
for_yanCommented:
Great!
0
 
learningunixAuthor Commented:
Excellent
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.