0

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());
user1436285
  • 291
  • 1
  • 3
  • 4
  • 2
    Which line has the NullPointerException? – John Watts Jun 05 '12 at 00:52
  • 2
    I think that line where you're getting `NullPointerException` is: `String mainPath = r.getPath()` - this indicates that class loader didn't find this file. This is probably classpath problem. – Xeon Jun 05 '12 at 00:58

1 Answers1

4

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.

tibo
  • 5,326
  • 4
  • 37
  • 53