?
Solved

Problem with executing jar file

Posted on 2006-03-20
30
Medium Priority
?
342 Views
Last Modified: 2010-03-31
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

0
Comment
Question by:vihar123
  • 13
  • 9
  • 5
  • +1
28 Comments
 
LVL 6

Expert Comment

by:avinthm
ID: 16244344
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
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16244347
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244377
>> 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:vihar123
ID: 16244388
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244400
>> 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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244403
>> its giving "NoClassDefFound" error..

With what name did you try? What is the full error message?
0
 

Author Comment

by:vihar123
ID: 16244404
My folder structure is \classes\Sample.class.
0
 

Author Comment

by:vihar123
ID: 16244408
>>With what name did you try? What is the full error message?
Exception in thread "main" java.lang.NoClassDefFoundError: Kc
0
 
LVL 6

Expert Comment

by:avinthm
ID: 16244410
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244413
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
 

Author Comment

by:vihar123
ID: 16244414
Sorry..
>>With what name did you try? What is the full error message?
Exception in thread "main" java.lang.NoClassDefFoundError: Sample
0
 

Author Comment

by:vihar123
ID: 16244425
>>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
 
LVL 6

Expert Comment

by:avinthm
ID: 16244432
then u will have to put Sample.class in kc directory and add the following in manifest file

Main-Class: kc.Sample
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244434
>> 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
 
LVL 6

Expert Comment

by:avinthm
ID: 16244437
your current directory should be classes, when creating the jar file
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244440
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244457
Can you post your complete directory structure for all files (source-code and class)?
0
 

Author Comment

by:vihar123
ID: 16244504
>>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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244521
>> 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
 

Author Comment

by:vihar123
ID: 16244555
Where can i keep Manifest.txt file?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244561
Keep it in the ex folder.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244564
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
 

Author Comment

by:vihar123
ID: 16244588
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
 
LVL 30

Accepted Solution

by:
Mayank S earned 1200 total points
ID: 16244602
>> 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
 

Author Comment

by:vihar123
ID: 16244748
Ok Thanks its working fine..
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16244761
Great. Any reason for the B-grade?
0
 
LVL 6

Expert Comment

by:avinthm
ID: 16268120
Didnt any of my comments help you in sorting out your problem ?

can i expect some points ? ;)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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