Runtime.getRuntime().exec(__);

1.5k views Asked by At

I'm rather new to Java and I'm creating my first project.

Anyway - I'm trying to execute another compiled program located in the same folder using Runtime.getRuntime().exec(__);

Thing is - When running and entering the necessary information for the rest of the program and reaching the point of execution, I get a sort of error.

java.io.IOException: Cannot run program "\Italian": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at java.lang.Runtime.exec(Runtime.java:620)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at Login.main(LOGIN.java:24)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:386)
at java.lang.ProcessImpl.start(ProcessImpl.java:137)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 4 more

The main CODE is -

import java.util.Scanner;
import java.io.*;
import javax.swing.JOptionPane;
class Login {

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    String username;
    String password;
    String a = "\\Italian";

    username = JOptionPane.showInputDialog(null,"Log in:\nEnter username: ");
    password = JOptionPane.showInputDialog(null,"Enter Password: ");

    users check = new users(username, password);



    if(check.auth()) 
        try
        {
            Runtime.getRuntime().exec(a);
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }


    }

}
2

There are 2 answers

0
Hovercraft Full Of Eels On

The error message is telling you exactly what is wrong -- your path to file of interest is wrong. To find out what the correct path is, add to your program:

System.out.println("user dir path: " + System.getProperty("user.dir"));

And then use a path relative to the path shown.

Also, does the Italian file have an extension such as .exe? Else, how will it run? When calling Runtime.getRuntime().exec(a);, a needs to represent an executable collection of Strings, often an array or ArrayList that sometimes requires calling the OS's command directly. Also as a side rec, look into using a ProcessBuilder for obtaining your Process, and don't forget handling the Process's streams.

0
user15868374 On

What opsys are you running on, android, linux, windows?

On android, in terminal, if it's a binary that has permissions you just type the command.

If it's on a drive that does not have execution permission, like the sdcard, you type "sh "

If it's windows, type the full path before the command: "c:/folder/runcmd.exe"