Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java项目在eclipse中运行,但在jar文件中不正确_Java_Eclipse - Fatal编程技术网

java项目在eclipse中运行,但在jar文件中不正确

java项目在eclipse中运行,但在jar文件中不正确,java,eclipse,Java,Eclipse,我面临着一个奇怪的问题。当我在eclipse中运行我的项目时,一切都很好。当我将项目导出到可执行的jar文件并运行它时,它只加载普通的JFrame 我应该如何发现问题?我不知道会出什么问题。我认为问题可能在这里,但不知道: try { URL url = getClass().getClassLoader().getResource("maps/"+ level +".txt"); File file = new File(url.getFile()); fr = new

我面临着一个奇怪的问题。当我在eclipse中运行我的项目时,一切都很好。当我将项目导出到可执行的jar文件并运行它时,它只加载普通的
JFrame

我应该如何发现问题?我不知道会出什么问题。我认为问题可能在这里,但不知道:

try {
    URL url = getClass().getClassLoader().getResource("maps/"+ level +".txt");
    File file = new File(url.getFile());
    fr = new FileReader(file);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
    in = new BufferedReader(fr);

您阅读资源的方式不正确。尝试使用以下方法:

    BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/maps/1.txt")));
    br.readLine();

尝试使用以下解决方案,System.getProperty()将为您提供当前目录路径:

-代码片段:

InputStream input = null;
try{
    String dirPath = System.getProperty("user.dir");
    String completePath= dirPath+fileName;
    input = this.getClass().getClassLoader().getResourceAsStream(completePath);
}catch(IOException ioex){
e.printStackTrace();
}

sry我不明白您的意思,我使用了绝对路径,它工作正常,谢谢。但是,如果没有绝对路径,如何实现它?不,您使用的是相对路径。您并没有以“/”:)开头,为了使用相对路径,您显然形成了相对于当前类的路径。我使用了像“C:\\Users\\Me…”这样的绝对路径,它以这种方式工作,但它只是一个糟糕的解决方法。类路径的绝对路径。哈哈。前导“/”的意思是你项目的根。我知道,但我告诉你,它在任何方面都不起作用。相关答案::)尝试过,当我打印出completePath路径正确时,但当我在jar中编译它时,它仍然不起作用