1

I have a JAVA GUI project in netbeans (with palette) ... The main folder is "ImageBrowser". It has these foldrers "build", "dist", "nbproject", "src", and these files "build.xml", "manifest.mf". Into the folder "src" there are the foldrers "Icons", "ImageBrowser" Into the "Icons" I have a picture, and in "ImageBrowser" are the sources.

FIRST PROBLEM

Inside the source code I use :

ImageIcon icon = new ImageIcon("src/Icons/my_photo_name.jpg");
labelImage.setIcon(icon);

Take a good look at the directory "src/Icons/my_photo_name.jpg" I ensure you that when I run the project from netbeans, the program shows the "my_photo_name.jpg", whith no problems.

When I make the jar (I made it in netbeans in Linux) and run it in windows7, the program DO NOT SHOW the "my_photo_name.jpg", obviouly it cannot find it !

Can you tell me why ?

SECOND PROBLEM

When I run the jar in Linux terminal it sais:

Exception in thread "main" java.lang.UnsupportedClassVersionError:
ImageBrowser/Main : Unsupported major.minor version 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:643)
at
java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: ImageBrowser.Main. Program will exit.

I will ensure you that from project proprties i have defined the main class....

Anyone who help me, he will save me !! Thanks in advance !

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
  • 2
    First issue, you need to use a resource loader. Second issue, you're attempting to run Java 8 files with java 7. – christopher Jul 22 '14 at 18:51
  • 1. using a relative file name like that will only work if you execute the jar file from the directory containing that path. Another directory (not to mention another machine) and it won't work. As for number two, that means you have more than one version of java installed, and you're using a newer version to compile than you are to run. – zebediah49 Jul 22 '14 at 18:52
  • @christopher : What is resource loader, and how can I use it ? – Mylonas Thomas Jul 22 '14 at 18:57
  • @zebediah49 : My friend what is my fault about what I did ? What directory could write, I with this that I use, from what folder, can I run my jar .... I thought java is for any platform .... I cannot understand ... As you see inside the netbeans the the image is found, and from jar not ... What directory I have to write ? – Mylonas Thomas Jul 22 '14 at 19:03
  • Anyway thanks to everyone for you quick answers !! :) – Mylonas Thomas Jul 22 '14 at 19:03
  • You could have really used more sensible title of the question. Also, questions aren't that much related to each other. Splitting those into separate questions would have been way more appreciated. – Kamiccolo Jul 22 '14 at 19:04
  • When you say the image is in `"src/Icons/my_photo_name.jpg"`, you are actually saying it is `"/src/Icons/my_photo_name.jpg"`. This is a problem, because that only works when "" is your netbeans folder. @Mentok's answer describes how you have to use a resource loader to fix that. As for Java versions -- Java has more than one version. If you make your program with a new version of Java/Netbeans, it won't run on an old version. See http://stackoverflow.com/questions/9290848/how-to-set-a-java-compiler-in-netbeans for a way to fix that. – zebediah49 Jul 22 '14 at 19:09
  • Friends the 2nd problem solved, because I found where is my java 8 and run it .... The 1st ...I thought that when you make a jar, the system searches not in "/src/Icons/my_photo_name.jpg", but inside the zip file jar (java archive) .... I will see the issue with "resource loader" ... @Kamiccolo you have right about the title ... sorry !!! – Mylonas Thomas Jul 23 '14 at 08:41

1 Answers1

4

As the comments have mentioned, your program is looking for your image in a non-existent path on the windows machine (src/Icons where you execute your JAR file).

To use something like an image in your java program, use a resource loader. Here is a good overview.

Eventually you'll end up with something like:

ImageIcon icon = new ImageIcon(this.getClass().getResource("my_photo_name.jpg"));
Mentok
  • 597
  • 4
  • 6