Reputation: 734
I'm developing a tool which will take in an XML file by the user. The tool will perform some manipulation and create a new XML file which then the user can take and do whatever they please with it.
I want to place my jar in a folder (XMLTOOL) alongside user's XML file (input.xml). The hierarchy is as follows:
XMLTOOL
|__tool.jar
|__input.xml
During runtime, I wish to access the file input.xml (or any other XML file the user puts in this folder (e.g. through command line args: java tool newInput.xml
)
Currently I'm using:
ClassName.class.getResource(inFile);
Where ClassName
is the name of my class. However this only works if input.xml
is in the same package as my source files in Eclipse.
So what is the standard convention for structuring resource files in Java projects, keeping in mind that these resources should be accessible when running from a JAR archive.
Upvotes: 0
Views: 46
Reputation: 8783
The simplest way to address files in the local filesystem is through the java.io.File API:
String path=...
File file=new File(path);
// To read the file:
InputStream input=new FileInputStream(file);
// To write to the file:
OutputStream output=new FileOutputStream(file);
path
may be absolute or relative to the current directory.
When executing your application from Eclipse, the current working directory is a parameter of your launching: You can set it the arguments
tab from the run configuration
window. By default, it is the project's directory, but you might set your own custom directory.
Upvotes: 1
Reputation: 780
You can also create your own namespace say for example (D:\ .. \ Project \ Input \ .xml files) where you can place your xml files and fetch during run-time to avoid confusion.Then,you can use methods like getAbsolutePath() to get the path of xml files and run wherever u want,especially when u run from a JAR file.
You can also use like Maven tool,to manage your projects and directories in a structured and conventional way.
Upvotes: 0