quarta-feira, 4 de março de 2015

Java Class Path Accessing Resources Within Classpath Examples

Since i haven't found satisfying examples around the web i've decided to make one.

Suppose the following project classpath:



How access those ini files from within the class path?

public class ClassPathTest {

    public ClassPathTest() {
        tryAbsoluteClassPaths();
        tryRelativeClassPaths();
    }

    private void tryAbsoluteClassPaths() {

        try {
            InputStream absolutePathFile = this.getClass().getClassLoader().getResourceAsStream("test/subpackage/config1.ini");
            absolutePathFile.close();
            System.out.println(absolutePathFile.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

        try {
            InputStream absolutePathFile = this.getClass().getClassLoader().getResourceAsStream("test/config2.ini");
            absolutePathFile.close();
            System.out.println(absolutePathFile.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

        try {
            InputStream absolutePathFile = this.getClass().getClassLoader().getResourceAsStream("classpathtest/config3.ini");
            absolutePathFile.close();
            System.out.println(absolutePathFile.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

        try {
            URL url = this.getClass().getResource("/test/subpackage/config1.ini");
            System.out.println(url.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

        try {
            URL url = this.getClass().getResource("/test/config2.ini");
            System.out.println(url.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

        try {
            URL url = this.getClass().getResource("/classpathtest/config3.ini");
            System.out.println(url.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

    }

    private void tryRelativeClassPaths() {

        try {
            InputStream relativePathFile = this.getClass().getResourceAsStream("config1.ini");
            relativePathFile.close();
            System.out.println(relativePathFile.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

        try {
            InputStream relativePathFile = this.getClass().getClassLoader().getResourceAsStream("test/config2.ini");
            relativePathFile.close();
            System.out.println(relativePathFile.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

        try {
            InputStream relativePathFile = this.getClass().getClassLoader().getResourceAsStream("classpathtest/config3.ini");
            relativePathFile.close();
            System.out.println(relativePathFile.toString());
        } catch (Exception e) {
            System.err.println(e);
        }

        try {
            URL url = this.getClass().getResource("config1.ini");
            System.out.println(url.toString());
        } catch (Exception e) {
            System.err.println(e);
        }
     
        try {
            URL url = this.getClass().getResource("../config2.ini");
            System.out.println(url.toString());
        } catch (Exception e) {
            System.err.println(e);
        }
     
        try {
            URL url = this.getClass().getResource("../../classpathtest/config3.ini");
            System.out.println(url.toString());
        } catch (Exception e) {
            System.err.println(e);
        }
    }
}

The interesting is that this method:

this.getClass().getClassLoader().getResourceAsStream("classpathtest/config3.ini")

is supoosed to need a relative path reference while it only works using a full path "classpathtest/config3.ini" .

The only diferrence is that you can use this:

InputStream relativePathFile = this.getClass().getResourceAsStream("config1.ini");

While in the executing class is in the same path as the file.

The method:

URL url = this.getClass().getResource("../../classpathtest/config3.ini");

Shows how usually a relative reference is. Using "../" means you go 1 directory above from where you are. But in this case we are accessing the file from the project path and not from the java classpath.

If you want to try the project yourself you can download the netbeans project from here https://drive.google.com/file/d/0B2Qa5vruShPAa2hMdHpOX21lQWc/view?usp=sharing


Nenhum comentário:

Postar um comentário