How to open a packaged executable in the project?

Asked

Viewed 399 times

1

I am running a program as follows:

Runtime.getRuntime().exec("C:\\meuprograma.exe");

In which it works normally. But I would like to package the files (the executable and a few more) within my project so that the user doesn’t have to keep manipulating the files. Moreover, the file is important only at the time of execution, so there is no need for me to create a directory to "install" the necessary files (unless this is the only solution, of course). I created a structure similar to this:

src
 |__ br.foo.files
 |__ br.foo.main

Assuming my class calling the executable is inside the package main, how do I run a file within the package files? I tried to:

Runtime.getRuntime().exec("files/meuprograma.exe");
Runtime.getRuntime().exec("./files/meuprograma.exe");
Runtime.getRuntime().exec("/files/meuprograma.exe");

In which all generated the following exception:

Exception in thread "main" java.io.Ioexception: Cannot run program "files/myprogram.exe": Createprocess error=2, System cannot find the specified file

1 answer

1


Use:

File home = new File(System.getProperty("user.dir"));
File file2 = new File(home, "files/meuprograma.exe");
Runtime.getRuntime().exec(file2.getPath());

I put in the Github for future reference.

Documentation.

You can do it in a simpler way by concatenating the strings without creating the path real, but this is the most correct way.

  • In thesis you don’t need it. I don’t know if you have something specific from Netbeans creating a problem. What I did is essentially take the directory where the application is running. If that’s not what you want, I wouldn’t know what else to say. Tell me to remove the answer.

  • I would like to know what is the mistake here to have a -1?

  • what? No mistake, I believe.

  • @rrnan actually someone went out distributing -1 in my stuff.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.