java - How should path to some properties file look like if i want it run the project as jar file through command line -
this question has answer here:
- how access config file inside jar? 4 answers
i have simple project depends on jar file. jar file has single class constructor takes in path props.xml.
this project structure:
here main class:
import com.file.reader.filereader; public class simpleexample { public static void main(string[]args){ filereader rd = new filereader("props.xml"); } } here filereader.java
package com.file.reader; import java.io.file; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import org.w3c.dom.document; public class filereader { public filereader(string filename){ try { file file = new file(filename); documentbuilder dbuilder = documentbuilderfactory.newinstance() .newdocumentbuilder(); document doc = dbuilder.parse(file); system.out.println("root element :" + doc.getdocumentelement().getnodename()); if (doc.haschildnodes()) { system.err.println((doc.getchildnodes())); } } catch (exception e) { system.out.println(e.getmessage()); } } } this reads xml file.
filereader.java jar file being accessed in project. when run in eclipse see below output:
[#document: null] root element :company but when exported dummyfilepath jar file , tried running command line.
i see error being thrown:
c:\users\javaman\props.xml (the system cannot find file specified) from command line running
java -jar dummyfilepath.jar how can make run through command line
edit
after checking linked questions tried different way:
i moved props.xml src folder.
then changed simpleexample.java below:
import java.io.file; import java.net.url; import com.file.reader.filereader; public class simpleexample { public static void main(string[] args) { simpleexample se = new simpleexample(); system.err.println(se.getpath()); filereader rd = new filereader(se.getpath()); } public string getpath(){ url url1 = getclass().getclassloader().getresource("props.xml"); file f = new file(url1.getfile()); return f.getabsolutepath(); } } so when run in eclipse see below good:
c:\users\javaman\perforce\dummyfilepath\bin\props.xml [#document: null] root element :company when run same dummyfilepath.jar see below error:
c:\users\javaman\desktop>java -jar "c:\users\javaman\desktop\dummyfilepath.jar" c:\users\javaman\desktop\file:\c:\users\javaman\desktop\dummyfilepath.jar!\props.xml c:\users\javaman\desktop\file:\c:\users\javaman\desktop\dummyfilepath.jar!\props.xml (the filename, directory name, or volume label syntax incorrect)
since gave file class file name (indirectly through constructor), assumes meant relative path (relative current directory). in other words, it's equivalent .\props.xml, , since current directory on command line c:\users\javaman\ (which can see @ left of command prompt when execute java -jar dummyfilepath.jar), looks there. need specify absolute file path of props.xml.
for example, if props.xml in c:\users\javaman\someotherfolder, absolute path (in windows, @ least), c:\users\javaman\someotherfolder\props.xml.
Comments
Post a Comment