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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
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.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

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

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

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

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

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.
vihar123Author Commented:
Where can i keep Manifest.txt file?
Mayank SAssociate Director - Product EngineeringCommented:
Keep it in the ex folder.
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.
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
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 :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vihar123Author Commented:
Ok Thanks its working fine..
Mayank SAssociate Director - Product EngineeringCommented:
Great. Any reason for the B-grade?
avinthmCommented:
Didnt any of my comments help you in sorting out your problem ?

can i expect some points ? ;)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.