1

I'm trying to write a Java program which gets a executable file to run under Linux. This executable file receives two parameters and performs a nmap -sP operating with the two given parameters.

I've called this file as file.exe and its content is the one below:

nmap -sP $segment1-$segment1

I already did a chmod +x file.exe and it's in same directory where the .class is present.

The Java code is the following:

import java.lang.Runtime;
import java.lang.Process;
import java.io.IOException;
import java.lang.InterruptedException;

    public class runFile {

        public static void main (String args[]) throws IOException, InterruptedException {


            Runtime r = Runtime.getRuntime();
            Process p = r.exec("file.exe "+args[0]+" "+args[1]);
            p.waitFor();
        }


    }

After compiling, Whenever I try to run it (from where the file.exe is) by

java runFile

I'm getting the following exception and errors log:

Exception in thread "main" java.io.IOException: Cannot run program "file.exe": error=2, No such file or directory at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041) at java.lang.Runtime.exec(Runtime.java:617) at java.lang.Runtime.exec(Runtime.java:450) at java.lang.Runtime.exec(Runtime.java:347) at runFile.main(runFile.java:12) Caused by: java.io.IOException: error=2, No such file or directory at java.lang.UNIXProcess.forkAndExec(Native Method) at java.lang.UNIXProcess.(UNIXProcess.java:135) at java.lang.ProcessImpl.start(ProcessImpl.java:130) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022) ... 4 more

What am I missing?

1
  • Have you tried to specify the absolute path to file.exe? Commented Aug 24, 2013 at 21:49

2 Answers 2

2

The error is telling you that the executable can not be found in the current directory or the OS's search path.

Try including the Pathans part of the command

Process p = r.exec("/path/to/file.exe "+args[0]+" "+args[1]);

You should also consider separating each command/argument as a separate parameter

Process p = r.exec(new String[]{"/path/to/file.exe ", args[0], args[1]});

This will help with parameters that contain spaces.

You should also consider using ProcessBuilder, this will allow you to change the directory context that the command should be executed

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

2 Comments

It's telling me that exec has not a suitable exec(String, String, String) method.
Sorry, was thinking of ProcessBuilder, need to use String array
0

Your java program looks for file.exe in the directory where you started your java program. It does not look inside the directory with your class files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.