0

ボタンがあります。ボタンをクリックしてbatファイルのバックグラウンドを実行すると、フォルダーにファイルが生成されますが、Javaウィンドウはそこに残ります。

しかし、私のコードでは、batファイルを実行するためにJavaウィンドウを閉じる必要があります。

どこを変更する必要があるかを確認するのを手伝っていただけませんか。

コウモリの画面を見る必要はありません。ありがとう!

final JButton importMap = new JButton("Import");

    importMap.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent arg1) {

            //String osm = osmFile_path.replaceAll("\\","\\\\");
            System.out.println("You are going to import:"+osmFile_path);
            //Runtime.getRuntime().exec()
            try {
                FileWriter fw2 = new FileWriter("C:\\SUMO\\bin\\OSMTEST.bat");
                fw2.write("@echo off");
                fw2.write("\r\n");
                fw2.write("cmd");
                fw2.write("\r\n");
                fw2.write("set default_dir=C:\\SUMO\\bin");
                fw2.write("\r\n");
                fw2.write("start /b C:\\SUMO\\bin\\netconvert --osm-files="+osmFile_path+" --output-file="+osmnet_file);
                fw2.close();
                Runtime.getRuntime().exec("cmd.exe /C start /b C:\\SUMO\\bin\\OSMTEST.bat");
            } catch(IOException e) {
                e.printStackTrace();
            }
        }
    });

    content.add(importMap);
4

2 に答える 2

1

startパラメータで引数を使用しないでRuntime.getRuntime.exec()ください。指定されたコマンドを実行するための新しいウィンドウが開きます。

これはうまくいくはずです

Runtime.getRuntime().exec("cmd.exe /C C:\\SUMO\\bin\\OSMTEST.bat");

于 2013-03-03T17:03:07.493 に答える
0

実際に次のコードを使用する必要があります。

    try {
    FileWriter fw2 = new FileWriter("C:\\SUMO\\bin\\OSMTEST.bat");
    fw2.write("@echo off");
    fw2.write("\r\n");
    //fw2.write("cmd");//No need to specify this line
    fw2.write("\r\n");
    fw2.write("set default_dir=C:\\SUMO\\bin");
    fw2.write("\r\n");
    fw2.write("start /b C:\\SUMO\\bin\\netconvert --osm-files="+osmFile_path+" --output-file="+osmnet_file);
    fw2.write("\r\n");
    fw2.write("Exit");//To close bat file
    fw2.write("\r\n");
    fw2.close();
    Process process = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "C:\\SUMO\\bin\\OSMTEST.bat");//use the protoclhandler
    process.waitFor();//Waits the process to terminate
    if (process.exitValue() == 0)
    {
        System.out.println("Process Executed Successfully");
    }
} catch(Exception e) {//Process.waitFor() can throw InterruptedException
e.printStackTrace();
}
于 2013-03-03T17:19:03.817 に答える