John Smith
John Smith

Reputation: 6259

Cannot find json file in project folder

My project setup looks like this: enter image description here

Now i try to reference the report.json file like this within the MainFrameTest.java:

Template template = new JsonTemplate(Thread.currentThread().
            getContextClassLoader().getResource("/report.json").toURI()); 

I also tried:

Template template = new JsonTemplate(Thread.currentThread().
        getContextClassLoader().getResource("report.json").toURI());  

And the full path:

Template template = new JsonTemplate(Thread.currentThread().
        getContextClassLoader().getResource("C:/prohes/esc_print_test/report.json").toURI());  

But i always get this error:

java.lang.NullPointerException
at esc_print_test.MainFrameTest.<init>(MainFrameTest.java:28)
at esc_print_test.MainFrameTest.main(MainFrameTest.java:55)

What do i wrong? Thanks

Upvotes: 1

Views: 3704

Answers (2)

Kishan Bheemajiyani
Kishan Bheemajiyani

Reputation: 3439

Try to fine first current Directory then u can give the resources.

do this this will give u the current Address of folder.

String name = new File(".").getCanonicalPath();
System.out.println("Test "+ name);

and then u can have file, as you have defined path.

for getting new file in that path do something like this.

File file = new File(name, "/"+ "yourfilename.extension");

hope this helps.

and well another option is

URI file = new File(name, "/"+ "one.text").toURI();
            System.out.println(file);

Upvotes: 3

Vishnu
Vishnu

Reputation: 1009

Put the json file in src folder and use directly in getContextClassLoader().getResource("report.json").

EDIT:

I tried this in my eclipse:

public static void main(String arg[]){
        System.out.println("test="+Thread.currentThread().getContextClassLoader().getResource("report.json"));
    }

Output:

test=file:/D:/liferay/BundleTomcat/workspace/springBatch/target/classes/report.json

Just to be sure check the report.json file is present in the target folder where the .class files are present.

Upvotes: 1

Related Questions