This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes Jar File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Jar File" Watch "Jar File" New topic
Author

Jar File

Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
Hi,

I have application designed in eclipse, which i want to convert into jar file. I tried to export it as a Jar file from eclipse and it create a small file on my desktop but when i try to open the file it gives me an error: Failed to Load Main-Class Manifest Attribute from C:\Users\Maz\Desktop\Test.Jar

I believe i need a manifest file or something? I am new to this manifest file can you guys please advise?

Thank You
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I think you may have missed specifying which class would hold the main method in your jar file, and this may have happened by not completing a portion of the jar-creation wizard in Eclipse. When you're in the JAR Export dialog, Did you click "Finish" when you were done, or the "Next" button on the button? Check to make sure that you clicked "Next" and eventually got to the JAR Manifest Specification dialog where you tell Eclipse which class should be the Main class.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
Ah thanks. It works now but it doesnt display the Banner Image that i selected in the Jar file. Everything else is fine apart from the Banner image that is missing from the Jar file. Any ideas?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mayur Gosalia wrote:Ah thanks. It works now but it doesnt display the Banner Image that i selected in the Jar file. Everything else is fine apart from the Banner image that is missing from the Jar file. Any ideas?


Sorry, I can't find that section in the JAR creation wizard. What do you mean by Banner Image?
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
Hi,

I have used a Image in my application (Banner). The image is missing everywhere in the application, i select it while creating the jar file but for some reaosn it doesnt display it. However if i run the app from Eclipse the image is there.

This is my directory layout:

Program - workspace
Java System - Project (inside this folder is the image) and src folder
SRC folder has .java file which are my java files.

I select the files i need and its fine apart from that image.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
How do you refer to the image "file" held in the JAR? As a file? Or as a resource? It needs to be the latter to work in a JAR file. This issue is discussed in the JAR section of the Sun tutorials.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
Well, it in JAR file wizard where you have to select all the files, i select all the files and tick the "Export Generated class file and resources" and "Export Java Source Files and Resources". I dont see an option where it would allow me to select the image as a file or a resource. Sorry to be a nuisance.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19679
    
  18

JAR files are not Swing / user interface specific. Moving to Java in General.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mayur Gosalia wrote:Well, it in JAR file wizard where you have to select all the files, i select all the files and tick the "Export Generated class file and resources" and "Export Java Source Files and Resources". I dont see an option where it would allow me to select the image as a file or a resource. Sorry to be a nuisance.


No, I'm talking about in your code itself. Do you try to grab the images as files or do you use resources?

And no, you're not being a nuisance as these are decent questions, questions most of us have had to struggle with.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
I believe i am adding these a file i think not sure :



Thats the code i use to add the image so i believe its a file. Do you have link to tutorial or something that would allow me to change this to a resource?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mayur Gosalia wrote:I believe i am adding these a file i think not sure :
...


Yep, you're using files, and this is causing your retrieval of the images to fail. Question: where are your image files in relation to the class files? Often the images are in a subdirectory (sometimes named "images") of the directory that holds the class files.

e.g.,
getting resources
How to use Icons -- see "Loading Images Using getResource" section.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
My image is not in the sub directory, it just lies in the root of the project directory.

Folder called Project - this is my workspace
Folder called JavaSystem - this is the project folder

I have created a sub directory called images within the src folder now and added the image to the folder. Now using the throws a null pointer.


pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mayur Gosalia wrote:
I have created a sub directory called images within the src folder now and added the image to the folder. Now using the throws a null pointer.


Is the images subdirectory recognized as a package within Eclipse? And have this subdirectory/package and its contents been been selected and added to the JAR file?

What you need to do is to simplify your problem and solve it in isolation first. Create a simple Java class that displays a simple image and nothing else, and try to get this to work in a simple Jar file. Simply.
Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
Yes it has been recognised as a package in eclipse with 1 file (image file). Now i change my code in .java file to include this image and then run the file in eclipse to see if it is working and this is when i get null pointer exception. If i cant run the program in eclipse after changing code , i m sure jar file wont work either.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mayur Gosalia wrote:Yes it has been recognised as a package in eclipse with 1 file (image file). Now i change my code in .java file to include this image and then run the file in eclipse to see if it is working and this is when i get null pointer exception. If i cant run the program in eclipse after changing code , i m sure jar file wont work either.


Try getting rid of the class loader portion. Please have a look at my package structure and simple program here:



[Thumbnail for usingResources.JPG]

Mayur Gosalia
Ranch Hand

Joined: Jan 03, 2010
Posts: 54
TAHNK YOU SO MUCH PETE. I really appreciate your help.

ClassLoader cl = this.getClass().getClassLoader();
image = new ImageIcon(cl.getResource("images/Asda Logo.gif"));

I have used the above and your example of directory layout really helped. It is working now and all the image appear in the JAR file. Thank you for your patience, really appreciate it.

Thanks
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Mayur Gosalia wrote:TAHNK YOU SO MUCH PETE. I really appreciate your help.

You're quite welcome. Glad you have this solved.
 
wood burning stoves
 
subject: Jar File