Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行Runtime.getRuntime().exec(“文件路径”),但程序行为奇怪_Java_Exe - Fatal编程技术网

Java 运行Runtime.getRuntime().exec(“文件路径”),但程序行为奇怪

Java 运行Runtime.getRuntime().exec(“文件路径”),但程序行为奇怪,java,exe,Java,Exe,我正在为一些程序制作一个基于文本的启动器。我正在使用 Runtime.getRuntime().exec(“path//to//file.exe”)方法 该程序将运行,它总是会,但有些行为怪异或找不到关键的.dll文件。其他人在运行时把文本弄乱了。如你所见,这些都是复杂的游戏,使用了很多我不理解的引擎。谢谢 死区3:屏幕是黑色的,但游戏运行。只有显示器似乎不起作用 MW3:TeknoMW3.exe无法找到TeknoMW3.dll,即使它们位于同一文件夹中(启动它通常会使其正常运行) 无穷无尽的空

我正在为一些程序制作一个基于文本的启动器。我正在使用
Runtime.getRuntime().exec(“path//to//file.exe”)
方法

该程序将运行,它总是会,但有些行为怪异或找不到关键的.dll文件。其他人在运行时把文本弄乱了。如你所见,这些都是复杂的游戏,使用了很多我不理解的引擎。谢谢

死区3:屏幕是黑色的,但游戏运行。只有显示器似乎不起作用

MW3:TeknoMW3.exe无法找到TeknoMW3.dll,即使它们位于同一文件夹中(启动它通常会使其正常运行)

无穷无尽的空间:文本以奇怪的方式显示。Exit是%getuserexit%或类似于文本应该并且通常显示的每个空间的内容。仅出现在主菜单上

分割单元Blaclist:Dir正确,找不到指定的文件。我只想解决那个问题

Bioshock 1:直接崩溃(可能是由于一个.dll问题,但我不知道)

生物休克2:从不跑步。文件已执行,但未运行

public class Main {

static int launcherNum = 14;

static Launchers launchers[] = new Launchers[launcherNum];

static Launchers assassinsCreed3 = new Launchers("G://Games//ROMS//Assassins Creed III//AC3SP.exe", "Assassians Creed III (AC3)");
static Launchers bioshock = new Launchers("G://Games//ROMS//Bioshock//Builds//Release//Bioshock.exe", "Bioshock");
static Launchers bioshock2 = new Launchers("G://Games//ROMS//Bioshock 2//SP//Builds//Binaries//Bioshock2.exe", "Bioshock 2");
static Launchers bioshock3 = new Launchers("G://Games//ROMS//Bioshock Infinite//Binaries//Win32//BioShockInfinite.exe", "Bioshock Infinite");
static Launchers borderlands2 = new Launchers("G://Games//ROMS//Borderlands 2//Binaries//Win32//Borderlands2.exe", "Borderlands 2");
static Launchers mw3 = new Launchers("G://Games//ROMS//Call of Duty- Modern Warfare 3//TeknoMW3.exe", "Call of Duty: Modern Warefare 3 (MW3)");
static Launchers deadSpace3 = new Launchers("G://Games//ROMS//Dead Space 3//deadspace3.exe", "Dead Space 3");
static Launchers endlessSpace = new Launchers("G://Games//ROMS//Endless Space//EndlessSpace.exe", "Endless Space");
static Launchers prototype2 = new Launchers("G://Games//ROMS//PROTOTYPE 2//Prototype2.exe", "Prototype 2");
static Launchers leagueOfLegends = new Launchers("G://Games//ROMS//lol.launcher.exe", "League of Legends");
static Launchers rct3 = new Launchers("G://Games//ROMS//RCT3//RCT3plus.exe", "Rollercoaster Tycoon 3");
static Launchers splitSecond = new Launchers("G://Games//ROMS//Split  Second//SplitSecond.exe", "Split Second");
static Launchers skyrim = new Launchers("G://Games//ROMS//The Elder Scrolls V Skyrim//TESV.exe", "The Elder Scrolls: Skyrim");
static Launchers tcSplinterCell = new Launchers("G://Games//ROMS//Tom Clancys Splinter Cell  Blacklist//src//SYSTEM//Blacklist_DX11_game.exe", "Tom Clancy's: Splinter Cell Blacklist");

public static void initLauncher() {

    //adds AC3
    launchers[0] = assassinsCreed3;

    //Adds all 3 Bioshocks. God its hard to write "Bioshock"
    launchers[1] = bioshock;
    launchers[2] = bioshock2;
    launchers[3] = bioshock3;

    //Adds Borderlands 2
    launchers[4] = borderlands2;

    //Adds MW3
    launchers[5] = mw3;

    //Adds Dead Space 3
    launchers[6] = deadSpace3;

    //Adds Endless Space
    launchers[7] = endlessSpace;

    //Adds Prototype 2
    launchers[8] = prototype2;

    //Adds League of Legends... yeah, I know.
    launchers[9] = leagueOfLegends;

    //Adds RCT3
    launchers[10] = rct3;

    //Adds Split Second
    launchers[11] = splitSecond;

    //Adds skyrim
    launchers[12] = skyrim;

    //Adds Splinter Cell
    launchers[13] = tcSplinterCell;
}

public static void runLaunchers() {

    boolean done = false;
    String input1;
    Scanner input = new Scanner(System.in);

    while(done == false) {
        System.out.println("Welcome to the Cracked Game Launcher, also know     as CSteam. This is a product of the labors of ASIGTX. Do not redistribute.\n Ever");
        System.out.println("Please select a game from our libraires.");
        System.out.println();
        for (Launchers l: launchers) {  
            if (l.launcherName != null) {
            System.out.println(l.launcherName);
            System.out.println();
            }//end of if loop
        }//end of for loop

        input1 = input.nextLine();

        switch (input1) {

        case "Assassins Creed 3":
        case "AC3":
            launchers[0].launchEXE();
            if (launchers[0].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Bioshock":
            launchers[1].launchEXE();
            if (launchers[1].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Bioshock 2":
            launchers[2].launchEXE();
            if (launchers[2].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Bioshock Infinite":
            launchers[3].launchEXE();
            if (launchers[3].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Borderlands 2":
            launchers[4].launchEXE();
            if (launchers[4].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "MW3":
        case "Call of Duty: Modern Warefare 3":
            launchers[5].launchEXE();
            if (launchers[5].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Dead Space 3":
            launchers[6].launchEXE();
            if (launchers[6].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Endless Space":
            launchers[7].launchEXE();
            if (launchers[7].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Prototype 2":
            launchers[8].launchEXE();
            if (launchers[8].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "League of Legends":
            launchers[9].launchEXE();
            if (launchers[9].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Rollercoaster Tycoon 3":
            launchers[10].launchEXE();
            if (launchers[10].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Split Second":
            launchers[11].launchEXE();
            if (launchers[11].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "The Elder Scrolls: Skyrim":
        case "Skyrim":
            launchers[12].launchEXE();
            if (launchers[12].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        case "Tom Clancy's: Splinter Cell Blacklist":
        case "Splinter Cell Blacklist":
            launchers[13].launchEXE();
            if (launchers[13].hasLaunched == true) {
                done = true;
            }//end of if
            break;

        }//end of switch    
    }//end of while loop

}


public static void main(String[] args) {

    initLauncher();
    runLaunchers();

}//end of main

}//end of class





public class Launchers {

public boolean hasLaunched = false;
public String launcherName;

private String dir;


public Launchers(String dir, String namein) {
    setName(namein);
    this.dir = dir; 
}//end of constructor

public void setName(String s) {
    launcherName = s;
}//end of setname
public String getDir() {
    return dir;
}


public void launchEXE() {

    String runtimeName = launcherName;


    try {
        Runtime.getRuntime().exec( getDir() );
    } catch (IOException e) {
        System.out.println("Dir is invalid");
        e.printStackTrace();
    }

        hasLaunched = true;
        System.out.println(launcherName +" has launched!");

}//end of launchEXE


}

程序可能对其执行上下文有期望

从您的示例来看,很可能每个程序都是在您运行程序的相同上下文/执行位置启动的,这意味着它们找不到所需的库或资源

不要直接使用
Runtime#exec
,而是尝试使用。这将允许您更改流程的执行位置

例如

String cmd = getDir();
File cmdFile = new File(cmd);
// Maybe check that the cmdFile.exists...;)
File parentFile = cmdFile.getParentFile();

ProcessBuilder pb = new ProcessBuilder(cmd);
pb.directory(parentFile);
pb.redirectError();

Process p = pb.start();
还建议您读取进程的
InputStream
,因为如果您不这样做,某些进程会感到不安;)


你告诉我们他们的行为很奇怪,但没有提供其他细节?我们猜猜看吗?等一下,我来编辑我的问题。@SotiriosDelimanolis,看看最新情况,如果有人能解释-1票,这也会很有帮助为什么要创建一个CMD文件来运行我的.exe文件?我想创建
文件
,因为从中提取父目录比使用
子字符串
索引
更容易。如果您注意到,我只使用
parentFile
来设置执行上下文(程序“.”位置将变为的位置),并使用
getDir
中的
cmd
值来实际运行程序。。。
InputStream is = p.getInputStream();
// This simple reads the contents from the InputStream and discards it
// You could change it to actually dump the output if wanted ;)
while (is.read() != -1);

int exitValue = p.waitFor();