Solved

creating jar file

Posted on 2011-09-08
32
297 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:learningunix
  • 17
  • 13
  • 2
32 Comments
 

Author Comment

by:learningunix
ID: 36508489
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
 
LVL 3

Expert Comment

by:evedder
ID: 36508496
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
 

Author Comment

by:learningunix
ID: 36508508
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508525

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508527

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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508531


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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508535
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
 
LVL 3

Assisted Solution

by:evedder
evedder earned 100 total points
ID: 36508539
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508541

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
 

Author Comment

by:learningunix
ID: 36508567
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
 

Author Comment

by:learningunix
ID: 36508571
somehow
javac *.java worked
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36508574
you should go to C:\DELETE folder and from there
compuile

javac com/mt/project/A.java
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36508582
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
 

Author Comment

by:learningunix
ID: 36508587
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508592
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508594
If you want to do it manually - then follow the link as eveede recommended
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 47

Expert Comment

by:for_yan
ID: 36508599

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
 

Author Comment

by:learningunix
ID: 36508600
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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508605
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
 

Author Comment

by:learningunix
ID: 36508620
Can I do it manually instead of installing eclipse for java
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36508627
Yes, follow the link above - if you have JDK 6 - read the last paragraph - it is actually automatoically
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36508632


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
 
LVL 47

Expert Comment

by:for_yan
ID: 36508666
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
 

Author Comment

by:learningunix
ID: 36508717
I have java 1_5 version and the option e is invalid. let me try with the link you gave above
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36508726
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
 

Author Comment

by:learningunix
ID: 36508860
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
 

Author Comment

by:learningunix
ID: 36508884
there was just a typo. filename was   MAINFEST.MF
0
 

Author Comment

by:learningunix
ID: 36509010
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
 
LVL 47

Accepted Solution

by:
for_yan earned 400 total points
ID: 36509014

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
 

Author Comment

by:learningunix
ID: 36509030
finally I got it. Thanks a lot for your help and patience
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36509031
Great!
0
 

Author Closing Comment

by:learningunix
ID: 36509032
Excellent
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Fibonacci challenge 11 82
stringclean challenge 26 56
Which is the latest version of Apache Tomcatl 3 35
bitbucket vs gitbucket 3 32
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now