image = new JLabel(new ImageIcon("C:\\Users\\user\\AutoTextSum\\src\\autotextsum\\newImage.JPG"));

As you can see from the instantiation on the label above, I've used a static file referencing whereby I explicitly indicates the physical location. Let say this coding file name is autotextsum.java. How do I put the image without the need to explicitly indicates the file name?

image = new JLabel(new ImageIcon("newImage.JPG"));

I've tried this, but it just won't work. The main cause that I need to change the addressing mode because I need to create a .jar file, in which, the .jar needs to work in any other java enabled PC. But, the newImgae.JPG won't be at C:\Users\user\AutoTextSum\src\autotextsum in any other random PC.

P/S: autotextsum.java and newImage.JPG is in the same folder.

You sure that you put newImage.JPG in the proper directory? the second option is for relative path, you need to be sure that the relative path is correct.

The following code assumes an "images" folder in the jar, or an "images" folder in the folder hierarchy where the class files are stored - it works in both situations so you can test in normal folders, then build the jar without code changes.

URL imgURL = getClass().getResource("/images/" + "newImage.JPG");
// NB fileName is CASE SENSITIVE when accessing resources in a jar
image = new JLabel(new ImageIcon(imgURL));

The following code assumes an "images" folder in the jar, or an "images" folder in the folder hierarchy where the class files are stored - it works in both situations so you can test in normal folders, then build the jar without code changes.

URL imgURL = getClass().getResource("/images/" + "newImage.JPG");
// NB fileName is CASE SENSITIVE when accessing resources in a jar
image = new JLabel(new ImageIcon(imgURL));

Thanks a lot!!! works like magic

Please mark the thread as solved.

Last Q,

File file8 = new File("C:\\Users\\user\\AutoTextSum\\src\\autotextsum\\stoplist3.txt");

Process p = Runtime.getRuntime().exec("notepad.exe C:\\Users\\user\\AutoTextSum\\src\\autotextsum\\");

Is there any way to use the getResource for the above code segment? Tried the getResource on the above codes to replace the path, seems like it doesn't work.
getRuntime().exec needs the parameter to be a string form, so I've tried changed it to getRuntime().exec("notepad.exe " +url)

seems like by using getResource, "file:\" is added in front of the path.
I've tried,

String str = getClass().getResource("stopList3.txt").toString();
String fixedString = str.substring(6).replaceAll("%20", " ");

It still won't strip the file:\ infront of the actual path.
I wonder if this gives the problem? But getResource works easily on my image code.

all of the referencing has been resolved by stripping the "file:\"
but, when i created a .jar file, seems like the referencing still not readable.
(it runs during normal compilation, but not on .jar)

the following error occurs:
Exception in thread "main" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at autotextsum.AutoTextSumm.<init>(AutoTextSumm.java:341)
at autotextsum.AutoTextSumm.main(AutoTextSumm.java:652)

Anyone knows what it meant?

Looks like it can't find the file. Open the jar in your favourite zip utility and check the dir/file structure and names - NB inside a jar these names are case-sensitive (unlike in Windows).

I doubt that notepad will open stuff inside a jar, so maybe you need to copy that file to the system temp dir* and open it there

* System.getProperty("java.io.tmpdir")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.