user1436285
user1436285

Reputation: 291

why does getResource give different result?

Bellow is my code, when i run these code in IDE it works well, but when i run in dos command it gives me an error of NullPointException. Please give me a hand. thanks!

     //first get the classLoader
    ClassLoader classLoader = TestMainPath.class.getClassLoader();
    //show message
    System.out.println("loader=" + classLoader);
    //
    URL r = classLoader.getResource("TestMainPath/TestMainPath.class");
    System.out.println("r=" + r);
    String mainPath =r.getPath();
    System.out.println(mainPath);
    File sf = new File(mainPath + "/main/newfile");
    System.out.println(sf.getPath());
    System.out.println(sf.exists());

Upvotes: 0

Views: 76

Answers (1)

tibo
tibo

Reputation: 5494

Your problem is only because your IDE does not launch the app as you do. In your case the difference is on the classpath. Check how your IDE launch your program, which classpath it uses. Verify also that your class file is really where you expect it to be when you launch from the command line.

Upvotes: 4

Related Questions