ClassLoader in a virtual machine

I have the source code:

private Image getImage(String path, ClassLoader loader) {
    InputStream image = null;
    try {
        image = loader.getResourceAsStream(path);
        return new Image(PlatformUI.getWorkbench().getDisplay(), image);
    } finally {
        if (image != null) {
            try {
                image.close();
            } 
            catch (IOException e) {
                //OK
            }
        }
    }
}

On my computer, this code works fine. But in a virtual machine:

loader.getResourceAsStream(path);

always returns null. Why?

EDIT:

The path is a relative path. For example: icons / tools / device.png. The application that I am developing contains more than ten projects. I am using the Eclipse IDE. All projects have the structure:

  • com.pkg.name - folder with class icons
  • with icons and other files

Jar files also have this structure.

Works fine on my computer applications. On a virtual machine (Windows Server (64-bit)), the application cannot load images from a Jar file.

+3
source share
2 answers

, , ( ), , , ( CLASSPATH -cp ? CLASSPATH, , , )

, , .

,

./classes
./images
./libs

java -cp classes;images;libs/* my.app.Application (java 1.6 - ), , images/myImage.jpg.

, delimter classpath ; Windows : unix. , unix.

Edit

, ? , , :

this.getClass().getResourceAsStream(path)

Edit

, OSGi. , , eclipse, RCP. , .

OSGi , bundle1 bundle2, . . , . , classloader getImage, .

eclipse, , .

, , , () , ( )?

+1

, :

, () , *.class. ( , ) *.class , - .

Eclipse ( IDE), Eclipse src/ bin/.

0

Source: https://habr.com/ru/post/1730180/


All Articles