简体   繁体   English

如何从jar打开文件?

[英]How to open a file from jar?

I have a project where I open some XML files to read and edit information. 我有一个项目,在其中打开一些XML文件以读取和编辑信息。 This works fine when running in the IDE (Netbeans) but fails when I am building the project and running the .jar file. 在IDE(Netbeans)中运行时,此方法工作正常,但在构建项目并运行.jar文件时,此方法失败。 Is there a reason why this might not work? 有什么原因可能不起作用吗? The path of the project in Netbeans is Netbeans中项目的路径是

NetBeansProjects\project\src\data

but when I am running the jar file, this path is 但是当我运行jar文件时,此路径是

NetBeansProjects\project\build\classes

However, when I create the files in this folder, they still cannot be opened. 但是,当我在此文件夹中创建文件时,它们仍然无法打开。

The XML files need to be in the classpath when the jar file is executed. 执行jar文件时,XML文件必须位于类路径中。

You can give the classpath using -cp option when executing the jar file. 执行jar文件时,可以使用-cp选项提供类路径。 Similar question was answered here as well - Run a JAR file from the command line and specify classpath 这里也回答了类似的问题- 从命令行运行JAR文件并指定classpath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM