2

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 the 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?

Perception
  • 79,279
  • 19
  • 185
  • 195
user234194
  • 1,683
  • 10
  • 38
  • 56
  • Related: http://stackoverflow.com/questions/3721706/embedding-resources-images-sound-bits-etc-into-a-java-project-then-use-those – BalusC May 10 '11 at 21:21

1 Answers1

4

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

ajaest
  • 587
  • 4
  • 13
duffymo
  • 305,152
  • 44
  • 369
  • 561