We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Problem with executing jar file

vihar123
vihar123 asked
on
Medium Priority
403 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

Comment
Watch Question

Commented:
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 Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:

Author

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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> its giving "NoClassDefFound" error..

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

Author

Commented:
My folder structure is \classes\Sample.class.

Author

Commented:
>>With what name did you try? What is the full error message?
Exception in thread "main" java.lang.NoClassDefFoundError: Kc

Commented:
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 SPrincipal Technologist
CERTIFIED EXPERT

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?

Author

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

Author

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.

Commented:
then u will have to put Sample.class in kc directory and add the following in manifest file

Main-Class: kc.Sample
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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

Commented:
your current directory should be classes, when creating the jar file
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Can you post your complete directory structure for all files (source-code and class)?

Author

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 SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.

Author

Commented:
Where can i keep Manifest.txt file?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Keep it in the ex folder.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
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.

Author

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
Principal Technologist
CERTIFIED EXPERT
Commented:
>> 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 :)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Ok Thanks its working fine..
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Great. Any reason for the B-grade?

Commented:
Didnt any of my comments help you in sorting out your problem ?

can i expect some points ? ;)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.