Keith M
Keith M

Reputation: 1239

Java execute file relative to jar

I have a c# console application I will be sending commends to using Java, with the techniques described here Sending commands to a console application?

My issue is that the console application is in a folder that's in the same folder with the jar, and I can't find out how to execute a file relative to the same directory as the jar.

I tried a lot of things in Java, like

Process pr = Runtime.getRuntime().exec("runtime_libraries/consoleapp.exe");

But it says the file can't be found.

java.io.IOException: CreateProcess error=2, The system cannot find the file specified

I assumed it would be simple, but apparently it's not.

So my question is, how can I put a relative path into the Runtime exec?

Upvotes: 1

Views: 576

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347214

When ever you're faced with this kind of issue, you need to do some preliminary checks...

  1. Verify your current running location using System.out.println(System.getProperty("user.dir")); and make sure it's where you expect it to be;
  2. Verify the existence of the consoleapp.exe using System.out.println(new File("runtime_libraries/consoleapp.exe").exists()));

Most IDEs allow you to change the working directory during development. When in production, you should ensure that the working directory is set correctly (for example, under Windows, you can configure the short-cuts to "start" in a specified location)

Upvotes: 1

Related Questions