【发布时间】:2012-06-09 01:18:18
【问题描述】:
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());
【问题讨论】:
-
哪一行有 NullPointerException?
-
我认为你得到
NullPointerException的那一行是:String mainPath = r.getPath()- 这表明类加载器没有找到这个文件。这可能是类路径问题。
标签: java getresource