Link to home
Start Free TrialLog in
Avatar of sunshine737
sunshine737

asked on

Problem with executing jar file

Hello,

My current directory is d:/ex.
I have one more directory with name "classes" contains main class file(Sample.class).
The java file is under package "kc".
I made jar file from the current directory with "jar -cvf sample.jar * ".
How can i run Sample.class file.

Thanks

Avatar of avinthm
avinthm

The manifest file should contain
Main-Class: Sample
(i am assuming that Sample is not in a package)

then enter the command in prompt
java -jar sample.jar
Avatar of Siva Prasanna Kumar
if you are using windows Xp uyou can directly say open with java binary file.
else java -jar sample.jar
in particular check out this link it may be very help ful
http://www.javaworld.com/javaworld/javatips/jw-javatip120.html
>> I have one more directory with name "classes" contains main class file(Sample.class).
>> The java file is under package "kc".

If the Sample class is in the kc package, you should have a folder-structure: \kc\Sample.class

Also, in the Main-Class attribute in the Manifest.mf, you should write:

Main-Class: kc.Sample

Make sure you have at least one new-line after the last line in the manifest-file.
Avatar of sunshine737

ASKER

Thanks,

Its working fine, if i dont have any sub folders in my directory. But my main class is located in "classes" sub folder.
What should i write in manifest file.
I tried in many ways, its giving "NoClassDefFound" error..

>> if i dont have any sub folders in my directory. But my main class is located in "classes" sub folder.

Does the class belong to the classes package? If yes, you should use classes.Sample. Otherwise, you should JAR it from inside the classes folder and just use Sample.
>> its giving "NoClassDefFound" error..

With what name did you try? What is the full error message?
My folder structure is \classes\Sample.class.
>>With what name did you try? What is the full error message?
Exception in thread "main" java.lang.NoClassDefFoundError: Kc
If your main class is in classes directory and classes is not a package then
you  will have to go to classes directory and then create the jar file.

then you can run the jar file with the command
What is the package-name defined in your source-code (Sample.java)? Have you defined anything or not? Are you using some IDE like Eclipse or Netbeans?
Sorry..
>>With what name did you try? What is the full error message?
Exception in thread "main" java.lang.NoClassDefFoundError: Sample
>>What is the package-name defined in your source-code (Sample.java)? Have you defined anything or not? Are you using some IDE like Eclipse or Netbeans?
My package name in Sample.java is "kc". I am using just notepad.
then u will have to put Sample.class in kc directory and add the following in manifest file

Main-Class: kc.Sample
>> My package name in Sample.java is "kc"

Then the folder structure for the class-file should be as I explained in one of my earlier comments: \kc\Sample.class

JAR it from the parent-folder of the 'kc' folder and use kc.Sample in your Manifest.mf
your current directory should be classes, when creating the jar file
Where is the classes directory, BTW, with respect to your source-file's directory and the class-file's directory? Why did it come into picture in the first place?
Can you post your complete directory structure for all files (source-code and class)?
>>Can you post your complete directory structure for all files (source-code and class)?
d:\ex\Sample.java
package kc;
class Sample
{
      public static void main(String[] args)
      {
            System.out.println(args[0]);
      }
}

I compiled and kept i kept in
d:\ex\classes\Sample.class

And    d:\ex\classes\Manifest.txt
Manifest-Version: 1.0
Created-By: 1.5.0_01 (Sun Microsystems Inc.)
Main-Class: kc.Sample


And i am in   d:\ex\classes\
and i created jar file  with "jar cfm kc1.jar Manifest.txt Sample.class"

Then i executed "java -jar kc1.jar XYZ"

I got "Exception in thread "main" java.lang.NoClassDefFoundError: kc/Sample"

How can i solve my problem? pls help me.

>> I compiled and kept i kept in d:\ex\classes\Sample.class

You had to keep in d:\ex\kc\Sample.class

JAR it from the ex folder.
Where can i keep Manifest.txt file?
Keep it in the ex folder.
Have a look at http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html and the tutorial link which I posted earlier.
Yes this is ok.
But as per the directory structure i have to keep all class files in "classes" folder. And then i should be able to create jar file from someware and i should be able to run the main class from that jar file.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok Thanks its working fine..
Great. Any reason for the B-grade?
Didnt any of my comments help you in sorting out your problem ?

can i expect some points ? ;)