简体   繁体   English

Runtime.getRuntime().exec(__);

[英]Runtime.getRuntime().exec(__);

I'm rather new to Java and I'm creating my first project.我对 Java 比较陌生,正在创建我的第一个项目。

Anyway - I'm trying to execute another compiled program located in the same folder using Runtime.getRuntime().exec(__);无论如何 - 我正在尝试使用 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();
        }


    }

}

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? 另外,意大利语文件是否具有扩展名,例如.exe? Else, how will it run? 否则,它将如何运行? When calling Runtime.getRuntime().exec(a); 当调用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. 需要表示一个可执行的String集合,通常是一个数组或ArrayList,有时需要直接调用OS的命令。 Also as a side rec, look into using a ProcessBuilder for obtaining your Process, and don't forget handling the Process's streams. 另外,作为补充,请考虑使用ProcessBuilder获取您的Process,不要忘记处理Process的流。

What opsys are you running on, android, linux, windows?你在什么操作系统上运行,android,linux,windows?

On android, in terminal, if it's a binary that has permissions you just type the command.在 android 上,在终端中,如果它是一个具有权限的二进制文件,您只需键入命令。

If it's on a drive that does not have execution permission, like the sdcard, you type "sh "如果它位于没有执行权限的驱动器上,例如 sdcard,则键入“sh”

If it's windows, type the full path before the command: "c:/folder/runcmd.exe"如果是windows,在命令前输入完整路径:“c:/folder/runcmd.exe”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM