I have generated ba jar file from maven pom file and added the test data files in to target folder using resources tag in POM file.. Even though the files are there inside the target folder,jar is not able to find the files.
Scenario:
There is a 2 buttons in joptionpane and when user clicks on the button, file present inside the target folder should be copied ins to he user choosen directory. Code is working in eclipse, but when I execute the Jar file, getting FileNotFoundException
Eg code:
File file=new File("Resources/testdata/data.xls") ;
I'm getting FileNotFoundException here. I have given Resources
directory directly as it is added to target.. Is I missing anything??
Code
File temp=null;
try {
temp = new File(getClass().getClassLoader().getResource("testdata/ResetPassword_UI/New_User_Creation_Data.xls").toURI());
} catch (Exception e2) {
temp = new File("./src/main/resources/testdata/ResetPassword_UI/New_User_Creation_Data.xls");
}
POM
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>utilities.ResetPasswordUI</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<targetPath>${project.build.directory}/src/main/resources</targetPath>
<filtering>false</filtering>
<includes>
<include>testdata/*</include>
<include>testdata/ResetPassword_UI/*</include>
<include>locators.properties</include>
</includes>
</resource>
</resources>
Exception
Exception in thread "AWT-EventQueue-0" java.lang.FileNotFoundException: c://.../src/resources/testdata/ResetPassword_UI/New_User_Creation_Data.xls
at utilities.ResetPasswordUI$2.actionPerformed(ResetPasswordUI.java:168)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
New Find: when I extract the Jar file, I'm able to see the testdata folder,and files that I have added. but don't know how to use that files which present in the jar
inside jar
|_test data -> data files
|_other files
now I want to use that files which are located inside the jar. so that i can able to run irrespective of location and I can just share the Jar without any docs that I'm using inside.
Folder Structure inside Jar file based on this, is there any way to pass the valid file path to getResource method.