Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Adding resource files in jar file, plus a export option questions
Adding resource files in jar file, plus a export option questions [message #520833] Mon, 15 March 2010 14:24 Go to next message
Ivan Dejanovic is currently offline Ivan DejanovicFriend
Messages: 1
Registered: March 2010
Location: Belgrade, Serbia
Junior Member
Question one:

I am having trouble adding resource file in jar file with eclipse. The example code taken from book Core Java Volume 1 is

/**
 * 
 */
package practice;

import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.swing.*;

/**
 * @author ivan
 *
 */
public class Start {

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				ResourceTestFrame frame = new ResourceTestFrame();
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
			}
		});
	}
}

class ResourceTestFrame extends JFrame
{
	public ResourceTestFrame()
	{
		setTitle("ResourceTest");
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		
		JTextArea textArea = new JTextArea();
		InputStream stream = getClass().getResourceAsStream("about.txt");
		Scanner in = new Scanner(stream);
		
		while (in.hasNext())
		{
			textArea.append(in.nextLine() + "\n");
		}
		
		add(textArea);
	}
	
	public static final int DEFAULT_WIDTH = 300;
	public static final int DEFAULT_HEIGHT = 300;
}



The code is rather simple. I have created about.txt file placed it in the bin/practice/ directory with class files. When I start the application from eclipse application work properly. When I start it via command line application work properly. When I export the application to jar file from eclipse the about.txt is not inside the jar file and I have to add it to jar file manually.

Can I add all the resource files to jar when I export the application from eclipse or do I have to use the command line to create the jar file?

Question two:

When I try to export my application to jar at one point I have a choice between:

Extract required libraries into generated JAR
Package required libraries into generated JAR
Copy required libraries into sub-folder next to the generated JAR

I am writing this from work where I have to use Windows so options may differ for my Linux version of eclipse at home but I know there are three options on both systems.

Question is what are there options do exactly. I tried them all both under Linux at home and under Windows at work and I am not clear what they do exactly.

If somebody could help me with these problems I would appreciate it. If I was unclear tell me and I will try to explain better.

Thanks in advance.
Re: Adding resource files in jar file, plus a export option questions [message #520910 is a reply to message #520833] Mon, 15 March 2010 17:35 Go to previous messageGo to next message
Steffen Zschaler is currently offline Steffen ZschalerFriend
Messages: 266
Registered: July 2009
Senior Member
For Question 1, you may consider putting about.txt under src/ instead.
Eclipse will copy it into bin appropriately, and will then also know to
include it when creating Jars etc.

Steffen

On 15/03/2010 14:24, Ivan Dejanovic wrote:
> Question one:
>
> I am having trouble adding resource file in jar file with eclipse. The
> example code taken from book Core Java Volume 1 is
>
>
> /**
> * */
> package practice;
>
> import java.awt.*;
> import java.io.*;
> import java.net.*;
> import java.util.*;
> import javax.swing.*;
>
> /**
> * @author ivan
> *
> */
> public class Start {
>
> /**
> * @param args
> */
> public static void main(String[] args)
> {
> EventQueue.invokeLater(new Runnable()
> {
> public void run()
> {
> ResourceTestFrame frame = new ResourceTestFrame();
> frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> frame.setVisible(true);
> }
> });
> }
> }
>
> class ResourceTestFrame extends JFrame
> {
> public ResourceTestFrame()
> {
> setTitle("ResourceTest");
> setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
>
> JTextArea textArea = new JTextArea();
> InputStream stream = getClass().getResourceAsStream("about.txt");
> Scanner in = new Scanner(stream);
>
> while (in.hasNext())
> {
> textArea.append(in.nextLine() + "\n");
> }
>
> add(textArea);
> }
>
> public static final int DEFAULT_WIDTH = 300;
> public static final int DEFAULT_HEIGHT = 300;
> }
>
>
>
> The code is rather simple. I have created about.txt file placed it in
> the bin/practice/ directory with class files. When I start the
> application from eclipse application work properly. When I start it
> via command line application work properly. When I export the
> application to jar file from eclipse the about.txt is not inside the
> jar file and I have to add it to jar file manually.
>
> Can I add all the resource files to jar when I export the application
> from eclipse or do I have to use the command line to create the jar file?
>
> Question two:
>
> When I try to export my application to jar at one point I have a
> choice between:
>
> Extract required libraries into generated JAR
> Package required libraries into generated JAR
> Copy required libraries into sub-folder next to the generated JAR
>
> I am writing this from work where I have to use Windows so options may
> differ for my Linux version of eclipse at home but I know there are
> three options on both systems.
>
> Question is what are there options do exactly. I tried them all both
> under Linux at home and under Windows at work and I am not clear what
> they do exactly.
>
> If somebody could help me with these problems I would appreciate it.
> If I was unclear tell me and I will try to explain better.
>
> Thanks in advance.
Re: Adding resource files in jar file, plus a export option questions [message #523678 is a reply to message #520910] Sun, 28 March 2010 15:01 Go to previous messageGo to next message
Greg Steele is currently offline Greg SteeleFriend
Messages: 1
Registered: March 2010
Junior Member
Steffen Zschaler said that Eclipse will copy resource files from the src directory to the bin directory as appropriate, but I have never seen this. I have been looking for a way to load an image resource into an app that will work both within Eclipse as I am coding/testing and from an exported jar file when I deploy the app.

From what I can see, Eclipse sets the Project/bin directory as the classpath when you are running an app from Eclipse. The image must be in the Project/bin directory in order for the ClassLoader to find it when you run the app within Eclipse.

When Eclipse builds a runnable jar, it copies resource files into the jar from the Project/src directory, NOT the Project/bin directory. If your resources are just in the bin directory, they won't get copied to the jar and your app won't work. You therefore have to maintain a copy of the files in BOTH the src and the bin directory in order for the app to work both in Eclipse and from a jar file.

Am I missing something? It seems to me that it would have been more logical for Eclipse to copy the resource files from the Project/bin directory rather than the Project/src directory. Then I wouldn't have to maintain two copies of the files and make sure they are in sync.
Re: Adding resource files in jar file, plus a export option questions [message #523713 is a reply to message #523678] Sun, 28 March 2010 18:44 Go to previous message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
Greg Steele wrote:
> Steffen Zschaler said that Eclipse will copy resource files from the src
> directory to the bin directory as appropriate, but I have never seen
> this. I have been looking for a way to load an image resource into an
> app that will work both within Eclipse as I am coding/testing and from
> an exported jar file when I deploy the app.
>
This has been a part of Eclipse from the beginning. In order to get
files onto the runtime classpath, Eclipse will copy non .java files to
the output folder in order to allow for Java apps to access them. The
folder containing the file has to be in a Java project source folder.

> From what I can see, Eclipse sets the Project/bin directory as the
> classpath when you are running an app from Eclipse. The image must be in
> the Project/bin directory in order for the ClassLoader to find it when
> you run the app within Eclipse.
>
> When Eclipse builds a runnable jar, it copies resource files into the
> jar from the Project/src directory, NOT the Project/bin directory. If
> your resources are just in the bin directory, they won't get copied to
> the jar and your app won't work. You therefore have to maintain a copy
> of the files in BOTH the src and the bin directory in order for the app
> to work both in Eclipse and from a jar file.
> Am I missing something? It seems to me that it would have been more
> logical for Eclipse to copy the resource files from the Project/bin
> directory rather than the Project/src directory. Then I wouldn't have to
> maintain two copies of the files and make sure they are in sync.
>
Previous Topic:Goldman Sachs Vice Presidents Speak Java at Great Indian Developer Summit 2010
Next Topic:getting methods from IFILE
Goto Forum:
  


Current Time: Thu Apr 25 18:54:18 GMT 2024

Powered by FUDForum. Page generated in 0.02827 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top