Results 1 to 4 of 4
- 05-06-2011, 05:23 AM #1
newbie
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Jar file making - picture problem
Hi everyone!
I have a big problem. Me and my team made a java project (for our studies), and i need to make a jar of it.
I tell you what i've tried (and then how i've fall):
I have a package named "graph", so i have a folder named "graph".
In this folder i have several source files (*.java), and 2 other folder named "model" and "images". The folder "model" contains more sourcefile, and the folder "images" contains some *.jpg picture, what i need for my program.
I made a build.bat and a run.bat file with these containments:
build.bat:
run.bat:Java Code:javac graph/*.java graph/model/*.java jar cvfe app.jar graph/Control graph/*.class graph/model/*.class graph/images/*.jpg
The program is running, but i have a big problem: i can't see the pictures...Java Code:java -jar app.jar
In one of the files whitch uses a picture i use it like this:What could be the problem?Java Code:icon = new ImageIcon("images/switchfalse.jpg");
Please help!
Thanks,
Amman
- 05-06-2011, 06:49 AM #2
The ImageIcon constructor you use requires a String that represents a file name. A resource inside a jar isn't a file.
Learn here how to construct an ImageIcon from an image in a jar, using a URL obtained from getResource(...)
How to Use Icons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)
dbLast edited by DarrylBurke; 05-06-2011 at 06:50 AM. Reason: Typos...
- 05-06-2011, 11:22 AM #3
newbie
- Join Date
- May 2011
- Posts
- 2
- Rep Power
- 0
Hi!
I tried to figure out how to do this magic with the resources, but i couldn't.
Can you help me?
Here is the code, where i'm using the picture:
This works, if i run in under eclipse.Java Code:package graph; import graph.model.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class SwitchView extends JLabel{ private Switch sw; ImageIcon icon; SwitchView(String viewname, Switch newsw) { super(viewname); sw = newsw; //java.net.URL imageURL = java.lang.ClassLoader.class.getResource("images/switchfalse.jpg"); icon = new ImageIcon("switchfalse.jpg"); //icon = new ImageIcon(imageURL); super.setIcon(icon); this.addMouseListener(new MouseAdapter(){ public void mouseClicked(MouseEvent e) { SwitchView swview = (SwitchView)e.getSource(); if(swview.sw.getValue()) { swview.setIcon(new ImageIcon("switchfalse.jpg")); swview.sw.switchSwitch(); } else { swview.setIcon(new ImageIcon("switchtrue.jpg")); swview.sw.switchSwitch(); } } }); } }
But now i tried to replace theline withJava Code:icon = new ImageIcon("switchfalse.jpg");but i'm getting nullpointerexception for the line "icon = new ImageIcon(imageURL);"Java Code://java.net.URL imageURL = java.lang.ClassLoader.class.getResource("images/switchfalse.jpg"); icon = new ImageIcon(imageURL);
To refresh the info about the folder hierarchy in the jar:
/graph <-sourcefiles
/graph/model <-sourcefiles
/graph/images <-images
- 05-06-2011, 12:14 PM #4
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
Similar Threads
-
Picture not showing when create JAR file
By kmckinley820 in forum New To JavaReplies: 4Last Post: 03-19-2011, 10:11 PM -
Making a picture Warhol Style
By chick in forum New To JavaReplies: 4Last Post: 04-13-2010, 09:08 PM -
making a vCard file
By Hussain Ali in forum Advanced JavaReplies: 2Last Post: 02-24-2010, 04:22 AM -
Picture in a JFrame problem
By saytri in forum New To JavaReplies: 3Last Post: 01-12-2008, 09:44 AM -
Print a picture file
By oli001 in forum New To JavaReplies: 0Last Post: 11-26-2007, 01:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks