Problem accessing a file at the root of the JAR

109 views
Skip to first unread message

Sheldon

unread,
Dec 12, 2011, 5:08:05 PM12/12/11
to onejar
I have a maven build using one-jar to create an executable test JAR.
The JAR runs just fine, but during the execution of my main class I
need to load my TestNG xml from the root of the JAR, but this fails.
This mechanism works with an executable zip on another project. I
would prefer to use one-jar so that I don't have to manually manage
the dependencies.
I've confirmed that files exist in the myProject.jar and the paths
are:
/com/address/MyMain
/myFile.xml
I added the following to the MyMain class:
...
System.out.println("This is the path: " +
MyMain.class.getClassLoader().getResource("myFile.xml").toString());
ArrayList<String> suites = new ArrayList<String>();
String xmlPath =
MyMain.class.getClassLoader().getResource("myFile.xml").toString();
suites.add(xmlPath)
testng.setTestSuites(suites);
...
When I run the JAR I get this as an output:
This is the path: jar:file:/myProject.one-jar.jar!/main/
myProject.jar!/myFile.xml
java.io.FileNotFoundException: jar:file:/myProject.one-jar.jar!/
main/myProject.jar!/myFile.xml (No such file or directory)

I tried other paths but cannot find one that works.

Any idea how I can get the path for the xml file?

Reply all
Reply to author
Forward
0 new messages