1

I am making a game with java as kind of a side project and am still new to the language. I was wondering how I could run a file when i didn't know the complete path, like if I were to send the game to a friend, and he has a different path location than me. Thank you in advance for any help.

code I am currently using:

File file = new File("/Users/(my name)/Desktop/script1.vbs");
Desktop desktop = Desktop.getDesktop();
if(file.exists()) desktop.open(file);`
GhostCat
  • 137,827
  • 25
  • 176
  • 248
Uni
  • 13
  • 3
  • possibile duplicate of http://stackoverflow.com/questions/43249167/java-how-to-get-the-correct-absolute-path-of-a-file-within-a-project/43249346#43249346 – freedev Apr 11 '17 at 13:36

3 Answers3

1

You could place the file (script1.vbs) in the project folder, that way the path would always be like this...

File file = new File("script1.vbs")

Place the file, not in the src or bin folder, but in the root folder.

frederick99
  • 1,033
  • 11
  • 18
vanillaSugar
  • 523
  • 1
  • 5
  • 14
0

Instead of

File file = new File("/Users/(my name)/Desktop/script1.vbs");

you should better use

File file = new File(System.getProperty("user.home"), "Desktop/script1.vbs");

See also the list of System Properties

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
0

Or you can use this, to locate your file in the resources folder:

File file = new File("classpath:com/company/script1.vbs");

"classpath:Route Of Source folders/Name and extension of the file"

MaQuiNa1995
  • 431
  • 1
  • 9
  • 23