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?

Uday
  • 1,433
  • 10
  • 36
  • 57
  • 1
    Please follow the [contribution guideline](https://stackoverflow.com/help/how-to-ask) how to ask a good question. Please do not post images of your code. – flaxel Sep 26 '20 at 11:10
  • Does this answer your question? [How to define a relative path in java](https://stackoverflow.com/questions/14209085/how-to-define-a-relative-path-in-java) – flaxel Sep 26 '20 at 11:15
  • @flaxel I have posted the image because anybody can understand the my file or folder structure. – Uday Sep 26 '20 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? – Abra Sep 26 '20 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](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar/) can help you. The method `getResource` could be helpful. – flaxel Sep 26 '20 at 11:22
  • @flaxel I am wondering i have extracted the jar and couldnt find my .xml file. May i know why it is not included? – Uday Sep 26 '20 at 11:27
  • It is not part of the build path. But you can add a [source folder](https://stackoverflow.com/questions/18091046/creating-runnable-jar-with-external-files-included/) to add the xml file. – flaxel Sep 26 '20 at 11:34
  • @flaxel i have created and moved the file under '/src/test/java/testng/' and created a SelAutomation.jar file. Now the xml file exists, but getting the same error 'java.io.FileNotFoundException: /Users/uanem/Downloads/src/test/java/testng/testng.xml (No such file or directory)' – Uday Sep 26 '20 at 11:50
  • Normally the test folder is not packed. Otherwise you can get the file with the [getResource](https://stackoverflow.com/questions/2593154/get-a-resource-using-getresource) method. – flaxel Sep 26 '20 at 11:58
  • @flaxel Could you please help how can i use getResource and get the file path and use here? Tried moving the file to src/main/resources, but didnt help. – Uday Sep 26 '20 at 12:22
  • Getting filenotfound when i use like this URL res = TestNGRunner.class.getClassLoader().getResource("/src/main/resources/testng/testng.xml"); File file = Paths.get(res.toURI()).toFile(); String filePath = file.getAbsolutePath(); – Uday Sep 26 '20 at 12:33
  • Following [this answer](https://stackoverflow.com/a/43415602/10951752) you can use the method like this: `File file = new File(getClass().getClassLoader().getResource("testing.xml").getFile());`. – flaxel Sep 26 '20 at 12:39
  • ***Never*** call the `getFile()` method of URL, ever. The proper way to read a resource is by using `getResourceAsStream` or by using the `openStream` method of the URL returned by getResource. A resource will not be a separate file when it’s part of a .jar file, and the getFile() method does not return a valid filename, it just returns a portion of a URL with all percent-escapes intact. – VGR Sep 26 '20 at 13:02

1 Answers1

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

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347