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

vihar123Asked:
Who is Participating?
 
Mayank SAssociate Director - Product EngineeringCommented:
>> But as per the directory structure i have to keep all class files in "classes" folder

Who said that? Generally many IDEs and app-servers use an 'src' folder for source-files and a 'classes' folder for class-files, to separate the two. There is no such rule that you should keep them in a classes folder. If you want to do so, you can keep in d:\ex\classes\kc\Sample.class. Keep the manifest in the classes folder and JAR from that folder. But I would suggest you don't get confused with all this, keep your basics clear :)
0
 
avinthmCommented:
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
0
 
Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
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
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
0
 
vihar123Author Commented:
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..

0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> its giving "NoClassDefFound" error..

With what name did you try? What is the full error message?
0
 
vihar123Author Commented:
My folder structure is \classes\Sample.class.
0
 
vihar123Author Commented:
>>With what name did you try? What is the full error message?
Exception in thread "main" java.lang.NoClassDefFoundError: Kc
0
 
avinthmCommented:
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
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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?
0
 
vihar123Author Commented:
Sorry..
>>With what name did you try? What is the full error message?
Exception in thread "main" java.lang.NoClassDefFoundError: Sample
0
 
vihar123Author Commented:
>>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.
0
 
avinthmCommented:
then u will have to put Sample.class in kc directory and add the following in manifest file

Main-Class: kc.Sample
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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
0
 
avinthmCommented:
your current directory should be classes, when creating the jar file
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Can you post your complete directory structure for all files (source-code and class)?
0
 
vihar123Author Commented:
>>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.

0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> 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.
0
 
vihar123Author Commented:
Where can i keep Manifest.txt file?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Keep it in the ex folder.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
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.
0
 
vihar123Author Commented:
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
0
 
vihar123Author Commented:
Ok Thanks its working fine..
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Great. Any reason for the B-grade?
0
 
avinthmCommented:
Didnt any of my comments help you in sorting out your problem ?

can i expect some points ? ;)
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.