从按钮打开cmd文件时出错

问题描述 投票:0回答:1

我创建了一个名为message.cmd的cmd文件,我想通过一个按钮打开他,他的内容是:

MSG * "this is a message"

我尝试用这段代码打开它:

Runtime.getRuntime().exec("cmd /c start lockComputer.bat");

它显示了一个错误:

“MSG”不被识别为内部或外部命令,可操作程序或批处理文件。

如果从项目文件夹中打开此文件,则可以正常工作。

此外,我尝试通过按钮在cmd文件中打开此代码,它工作正常:

rundll32.exe user32.dll , LockWorkStation

我能做什么?

java
1个回答
0
投票

我测试了它,它对我有用

package arash.blogger.example.thread;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

  /**
   * @param args
   * @throws IOException 
   */
  public static void main(String[] args) throws IOException {
    String sysRoot=System.getenv("WinDir");
    Process p=Runtime.getRuntime().exec("ping 127.0.0.1",new String[]{}, new File(sysRoot+"/System32"));
    BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
    String s;
    while( (s=br.readLine())!=null ){
      System.out.println(s);
    }
  }

}
© www.soinside.com 2019 - 2024. All rights reserved.