开发者

Java path to support both IDE and generated JAR

I have an issue with path names in my code. Let's say I have a main class:

com.test.LoadFile.java

Similarly I have a myxml.xml file under com.test. Meaning that th开发者_如何转开发e Java file and XML file are under same package.

Can somebody suggest how, when I do (inside LoadFile)

File file = new File("???/myxml.xml")  

What should the path be, to support both:

  1. Eclipse IDE code (after including the above code into a single Java project) and

  2. Run the main LoadFile class outside of the IDE (in a JAR file)

What should I use as the value of the path variable to include in the generated project JAR?


You can read the XML file using getResourceAsStream(), as long as it's in the CLASSPATH:

InputStream is = LoadFile.class.getClassLoader().getResourceAsStream("/myxml.xml");

EDIT: If you are packaging into a .jar, you must specify the complete path of the resource from the jar's root folder using "/" at the beginning of string

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜