0

Bellow 是我的代码,当我在 IDE 中运行这些代码时,它运行良好,但是当我在 dos 命令中运行时,它给了我一个 NullPointException 错误。请帮我一把。谢谢!

     //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());
4

1 回答 1

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.

于 2012-06-05T01:05:11.473 回答