1

I have two Java applications. One application will contain resource files and will be used as library to other Java application.

First app com.test.resourceusing.MainClass.java which contains res/base.xml resource file.

package com.test.resourceusing;

import java.io.BufferedInputStream;
import java.io.File;

import java.net.MalformedURLException;
import java.net.URL;

import java.util.Scanner;

public class MainClass {
public MainClass() {
    super();
}

public static void main(String[] args) {
    MainClass main = new MainClass();
    try {
        main.start();
    } catch (MalformedURLException e) {
    }
}

public void start() throws MalformedURLException {
    URL url = getClass().getResource("res/base.xml");
    System.out.println(url.getPath());
    System.out.println(url.getFile());
    File f = new File(url.getFile());
    if (f.exists()) {
        System.out.println("File exist!");

        BufferedInputStream result = (BufferedInputStream) 
              getClass().getResourceAsStream("res/base.xml");
        Scanner scn = new Scanner(result);
        while(scn.hasNext()){
            System.out.println(scn.next());
        }
    } else {
        System.out.println("Not working! :(");    
    }
}

}

Result is:

/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
File exist!
<?xml
version='1.0'
encoding='utf-8'?>
<schema>
</schema>

Then I create .jar file which contains all resource files and try to use it as library in other application.

Second app: resourcetest.MainClassTest.java

package resourcetest;

import com.test.resourceusing.MainClass;

import java.net.MalformedURLException;

public class MainClassTest {
  public MainClassTest() {
    super();
  }

  public static void main(String[] args) {
    MainClass main = new MainClass();
    try {
        main.start();
    } catch (MalformedURLException e) {
    }
  }
}

Result is:

file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml
Not working! :(

I don't understand why it's not working, is there problems in my code? Or this solution is not possible in Java?

Klavs
  • 47
  • 7

2 Answers2

1

Do you see the difference in the location of those files?

/C:/Work/Projects/ResourceUsing/classes/com/test/resourceusing/res/base.xml
file:/C:/Work/Projects/ResourceUsing/deploy/archive1.jar!/com/test/resourceusing/res/base.xml

You cannot access a resource that is located in a JAR file with the File API.

Your code is already on the way. A simple edit should work:

public void start() throws IOException {
    URL url = getClass().getResource("res/base.xml");
    if (url != null) {
        System.out.println(url.getPath());
        System.out.println(url.getFile());
        System.out.println("File exist!");

        try(InputStream result = url.openStream()) {
            try(Scanner scn = new Scanner(result)) {
                while(scn.hasNext()) {
                    System.out.println(scn.next());
                }
            }
        }
    } else {
        System.out.println("Not working! :(");    
    }
}
Seelenvirtuose
  • 20,273
  • 6
  • 37
  • 66
0

After deeper searching looks like I found a reason - https://stackoverflow.com/a/10605316/1117515. In this case I need to use getResourceAsStream().

Community
  • 1
  • 1
Klavs
  • 47
  • 7