0

enter image description here

From this image, when i run this class as Java application, it works perfectly fine.

I have created a executable jar file and saved in Downloads folder and when i run the jar, i am getting "java.io.FileNotFoundException: /Users/uanem/Downloads/testng.xml (No such file or directory)"

How to get my project's folder path and refer this testng.xml irrespective from where i am running jar file?

13
  • 1
    Please follow the contribution guideline how to ask a good question. Please do not post images of your code. Commented Sep 26, 2020 at 11:10
  • Does this answer your question? How to define a relative path in java Commented Sep 26, 2020 at 11:15
  • @flaxel I have posted the image because anybody can understand the my file or folder structure. Commented Sep 26, 2020 at 11:15
  • Pardon me if I am stating the obvious, but have you checked whether the JAR file you created contains file testing.xml? And if it does, is it in the path that appears in the error message? Commented Sep 26, 2020 at 11:21
  • I guess that the xml file is not exported with the jar. So you can copy the file to your specific location and use a realtive path or you can put it in the jar and maybe this post can help you. The method getResource could be helpful. Commented Sep 26, 2020 at 11:22

1 Answer 1

0

An executable jar has no memory of where you built it from, so there is no easy way to go back to the testng.xml file (why do you even need it?)

In the following I assume testng can look for this file in the classpath. (If you are unsure what that means, consider learning it - this is a very frequent problem for new programmers).

You can do one or more of the following:

  • Explicitly tell the path to the file on the command line. The exact syntax depends on the discovery mechanism used by testng. You may have to hand carry it from the args-string to the invocation point.

  • Add the Downloads folder to the class-path encoded in the executable jar manifest. That will not be portable to other systems, but may be enough for your personal use.

  • Move the file inside the classpath (src/java/resources) so it is included in your executable jar, and testng can locate it there.

  • Consider why this is needed in your executable jar, and if possible remove that functionality. Then the problem goes away.

  • Place it next to the executable jar and ask the JVM where the code is running. See https://stackoverflow.com/a/227640/53897

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.