1
votes

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?

2
Have you tried to specify the absolute path to file.exe? - Jk1

2 Answers

2
votes

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

0
votes

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.