2

This problem might have been answered before but I can't seem to make it work. Tried several different previously asked questions and answers.

I have an XML schema file in resource package, and need to get it as a File object from another package called ui.

It must the problem with setting relative path here, but I can't figure out how to fix it.

Class pacakge: com/abc/ui/myClass.java

Resource package: com/abc/resources/schema/XMLSchema.xsd

Sample Code:

    File schemaFile = null;
    try {
        schemaFile = new File(getClass().getClassLoader().getResource("../resources/schema/XMLSchema.xsd").getFile());
    } catch (Exception e) {
        e.printStackTrace();
    }

and/or

    File schemaFile = null;
    try {
        schemaFile = new File(getClass().getResource("../resources/schema/XMLSchema.xsd").getFile());
    } catch (Exception e) {
        e.printStackTrace();
    }

No matter what I try, it keeps throwing null value.

Update: Snap Shots: Class View

Class Package List

Error View

Any solutions to this.

Indigo
  • 2,887
  • 11
  • 52
  • 83
  • 1
    `XMLSchema.xsd` != `Schema.xsd` – Elliott Frisch Oct 01 '14 at 13:41
  • Sorry my typo mistake, corrected it. It wasn't a problem. I just tried to create a simple version of my code. – Indigo Oct 01 '14 at 13:41
  • Have you added the resources folder to the build path? – James Fox Oct 01 '14 at 13:42
  • @JamesFox: What do you mean by adding the folder to build path? It is the part of a Project under source in Netbeans, its not a separate folder. – Indigo Oct 01 '14 at 13:43
  • 1
    try `schemaFile = new File(getClass().getResource("XMLSchema.xsd").getPath());` – Mitesh Pathak Oct 01 '14 at 13:44
  • @MiteshPathak: nope still null :-( – Indigo Oct 01 '14 at 13:46
  • don't use the ClassLoader, just type: MyClass.class.getResource("../resource/XMLSchema.xml").getFile() – BilalDja Oct 01 '14 at 13:48
  • @BilalDja: `schemaFile = new File(getClass().getResource("../resources/schema/XMLSchema.xsd").getFile());` Still null – Indigo Oct 01 '14 at 13:51
  • Check your spell.. public static void main(String[] args) { System.out.println(new MyClass().fileExists("../resource/schema/XMLSchema.xml")); } private boolean fileExists(String url) { File XMLFile = new File(getClass().getResource(url).getFile()); return XMLFile.exists(); } – BilalDja Oct 01 '14 at 13:58
  • @BilalDja: unfortunately, still this is not working. I copied the path and used it but still no. Let me investigate more. – Indigo Oct 01 '14 at 14:16
  • @indigo: How about adding a snap short of your workspace – BilalDja Oct 01 '14 at 14:20

1 Answers1

4

There are two ways of using resources, via the class, or via the class loader.

The class:

SomeClass.class.getResource("/com/abc/resources/schema/XMLSchema.xsd")

The class loader

getClass().getClassLoader().getResource("com/abc/resources/schema/XMLSchema.xsd")

The difference is easy to see: the class loader uses absolute paths as it searches the class paths, several jars.

The class's getResource must start with a slash for absolute paths, and is relative to the package (directory) of the class. Because of inheritance getClass() might point to a child class package, so be careful there.

In your case try the more direct (jar limited) getClass().getResource. I would use an absolute path.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I don't why the relative path wasn't working but this solution worked pretty well. Strange, but happy to see this working. – Indigo Oct 01 '14 at 15:01
  • I think that this would solve the issue with the relative path not working: http://stackoverflow.com/questions/7598623/how-to-setup-classpath-in-netbeans – James Fox Oct 01 '14 at 15:06