30

I have searched for a method of embedding a resource in a java project (using Eclipse v3.6.0) then using that embedded resource within a control (e.g., JLabel). I have seen methods of referencing resources from the file system. Once the project has been developed I would like to publish the application as an executable. It should be noted these executables will be deployed/launched to Windows, *NIX, and Linux platforms.

I know this can be done in the Visual Studio world, but I'm very unfamiliar how to do this in Java/Eclipse IDE. As a side question, how do I get Eclipse to create the project as a executable so it can be launched?

Any help is greatly appreciated.

Mark

UPDATE 1:

Based upon BalusC's response, I wanted to share the code I have to resolve my problem. My classes are under the package of "Viking.Test" and then I placed the image file under the package "Viking.Test.Resources". This is all done within Eclipse to import the image into the project.

  1. I imported the image by right-clicking on the Project -> Import -> General/File System for the import source.
  2. Selected the folder which contained the image to import
  3. Selected "Project/src/Viking/Test/Resources" for the 'Into folder' parameter
  4. Didn't change any of the options and clicked "Finished"

In the source file I added the following code to insert the image into a JLabel (LblLogo)

try
{
  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  InputStream input = classLoader.getResourceAsStream(
    "Viking/Test/Resources/MyImage.jpg");
  Image logo = ImageIO.read(input);
  LblLogo = new JLabel( new ImageIcon( logo ) );
  LblLogo.setBounds(20, 11, 210, 93);
  getContentPane().add(LblLogo);
}
catch ( IOException e ) {  }

1 Answer 1

21

Just put those resources in the source/package structure and use ClassLoader#getResource() or getResourceAsStream() to obtain them as URL or InputStream from the classpath by the full qualified package path.

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream input = classLoader.getResourceAsStream("/image.gif");
// ...

Or if it is in the same package as the current class, you can also obtain it as follows:

InputStream input = getClass().getResourceAsStream("image.gif");

As a side question, how do I get Eclipse to create the project as a executable so it can be launched.

Rightclick Java Project > Export > Runnable JAR File .

4
  • Thanks BalusC, got it working. The one strange thing is the Right-Click Java Project -> Export -> Runnable JAR File works on one machine, but not the other. The machine that is doesn't work on I can issue the command "java -jar MyJarFile.jar" and it will run. During the exporting, if I select "Package required libraries into generated JAR" I get error message "Could not find the main class. Program will exit". Need to figure out why and I don't understand it at all.
    – lordhog
    Sep 16, 2010 at 8:00
  • Some things have changed since Eclipse 3.5. Which version is running at the machines? Easiest is to execute it once using Ctrl+F11 so that it get saved as last Run As configuration which you can pick during the export.
    – BalusC
    Sep 16, 2010 at 11:08
  • Okay, at home I was running Java v6 Update 21 and Java v6 Update 20 on the one that didn't work. I don't see why that would cause any concerns, but I uninstalled Java and re-installed Java. After I re-installed Java I was able to double-click and the jar file executed. Cool. I am not sure what the problem was. Now the other concern is that even when I close the window an instance of 'javaw.exe' is still running. Boy, I have to get use to this Java thing. Thanks for all the help BalusC!
    – lordhog
    Sep 16, 2010 at 16:35
  • Okay, with regards to fact that an instance of "javaw.exe" was still executing, I discovered that I needed to call the System.exit(0) function as described here.
    – lordhog
    Sep 17, 2010 at 18:25

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.