How to use the Eclipse plug-in development relative path calling picture?

Category: Eclipse
2013-08-20 03:30:39

Sponsored Links
Want to put the corresponding image *. Jar file, then how to use the Eclipse plug-in development relative path called picture it?
now the image on the generated class directory, run by ordinary application is no problem;
but if you run Equinox Framwork to die, can not find;
how to solve this problem ?
Thank you!

Sponsored Links

2013-08-20 03:40:00
posted a message to you right:

solution packaged into a Java program can not find the image and text files Jar problem

Problem Description:
When you write a Java program after release when packaged into a Jar, you will find the Jar file can not find the pictures and text files, the reason is the program load images or text file , used as a reference to the current working path way to specify files and paths. This Jar package when the user runs the current working directory is not consistent.

example: In Windows as an example, the following is a development project "AAA" part of the code development and debug the root path is D: \ aaa, this program runs When the current path is "D: \ aaa", we can use System.getProperty ("user.dir") method verify the current user working path.

/ / The following code uses a path based on the current work the way you specify a file object
File imageFile = new File ("/ images / sample.gif");
ImageIcon imageIcon = new ImageIcon (File.toURI);
File iniFile = new File ("/ conf.ini");
FileInputStream fileInputStream = new FileInputStream (iniFile.toURI);
/ *
* so that the program will go to the following two files:
* D: \ aaa \ images \ sample.gif
* D: \ aaa \ conf.ini
* /

code above application absolute path information Specify a file object, in the development and debugging phase (not packaged before), there is no problem, but in the package, due to all image files and text files will be packaged into the Jar file, because the System of "user.dir" ; attribute changes, will result in an absolute way path can not be found Jar file contains the path and file. For example, we will be above the project "AAA" all the class files, image files, and text files are packaged as E: \ aaa.jar file and publish, the user performs the aaa.jar the program, the current path depends on the user Run this program from the current path.

example: "E: \" directory, run the program:

E: \> java-jar aaa.jar

At this point the user's current path (System of user.dir property) is "E: \", rather than developers expect "E: \ aaa", it will search for the file in the following path:
E: \ images \ sample.gif
E: \ conf.ini
but all the pictures and text files included in the E: \ aaa.jar file, so will cause the program not run correctly.

problem solving:

In order to solve the above problems, it is recommended to use Java itself as the base class path search.
/ *
* The following code uses a class based on the current path in the manner specified. This line of code at runtime, it will
* jar file in the root path of the search program used files.
Reader reader = new InputStreamReader (
getClass (). getResourceAsStream ("/ image / Environment.ini"));
ImageIcon i = new ImageIcon (getClass (). getResource ("/ image/b1.gif"));

more than two of the following two lines of code using Java statements:
; getClass (). getResourceAsStream ("FileName");
getClass (). getResource ("File");
containing this code the program is running, the class (class path) as the base, instead of relying on the current path (System in user.dir).

NOTE: The above path "/ image / Environment.ini" if written as "image / Environment.ini", indicating similar path in the path (className / image / Environment.ini ), which requires you to have the picture and text file save path, and the path specified in the program both consistent.
2013-08-20 03:56:37
ah, the problem I have encountered this! about the situation is this: Here is my basic code and test code:
public class CopyDirectory {
public static final String PROJPATH = System.getProperty ("user.dir") ;/ / current project path
public static void main (String args []) {
System.out.println ("PROJPATH:" + PROJPATH) ;/ / project directory
if the direct way to run a Java Application The result is correct;
ie: D: \ eclipse \ workspaces \ AAA
but we all know that this is just a tool class, or is a constant class, when you use the Eclipse Application manner run the project, when it is called in other classes when the result is:
D: \ eclipse; Thus it is clearly not what I want up! that how I should get real The D: \ eclipse \ workspaces \ AAA, this path it?
everyone who can help solve
mail: [email protected]
2013-08-20 04:11:02
You want to get the current workspace path ecipse work, should be used ResourcesPlugin.getWorkspace (). getRoot (). getLocationURI (); to get to the workspace root path URI, URI can then be further convert even for further action.

Do not try to use System.getenv (), etc. to operate, should rely on eclipse platform itself provides content. If you attempt to access the workspace

resources should be used ResourcesPlugin class; Eclipse install directory if you attempt to access resources should be used Platform classes.
2013-08-20 04:14:40
Thank you very much!
may I just say that the problem is not clear to me now as if my code to run the application, it is possible to find a map, I'll put class graphics files between directories under;
My problem is that I want to develop is an OSGi-based plugin, but when running Equinox, you can not find the image;
Thank you very much!
continued attention and hope to get help!
2013-08-20 04:27:10
As said you have to get the resources jar package, if not the classpath, but also hope that through compressed to understand methods to achieve, then load the jar package can be loaded with any of the specified URL URLClassLoader the jar package, as long as this jar is loaded correctly.

want to help you.

eclipse plugin should rely on the principle of handling even the eclipse platform is OSGi, rather than the jvm.
2013-08-20 04:31:15
thank you!
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.