在Java中,我希望能够执行Windows命令.
有问题的命令是netsh.这将使我能够设置/重置我的IP地址.
请注意,我不想执行批处理文件.
我想直接执行这些命令,而不是使用批处理文件.这可能吗?
这是我实现的未来参考解决方案:
public class JavaRunCommand {
private static final String CMD =
"netsh int ip set address name = \"Local Area Connection\" source = static addr = 192.168.222.3 mask = 255.255.255.0";
public static void main(String args[]) {
try {
// Run "netsh" Windows command
Process process = Runtime.getRuntime().exec(CMD);
// Get input streams
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// Read command standard output
String s;
System.out.println("Standard output: ");
while …Run Code Online (Sandbox Code Playgroud) 我试图从java和我的C++程序中调用C++程序,如下所示:
// A hello world program in C++
// hello.cpp
#include<iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我所做的是minGW compiler 用于将C++程序编译为hello.exe,当我使用它时,它正在工作:
C:\Users\admin\Desktop>g++ -o hello hello.cpp
C:\Users\admin\Desktop>hello.exe
Hello World!
Run Code Online (Sandbox Code Playgroud)
我已经创建了一个java程序,它应该调用C++编译的程序(hello.exe),但我的java程序是注意调用exe,我的程序如下:
//Hello.java
public class Hello {
public static void main(String []args) {
String filePath = "hello.exe";
try {
Process p = Runtime.getRuntime().exec(filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
检查java程序的输出:
C:\Users\admin\Desktop>javac Hello.java
C:\Users\admin\Desktop>java Hello
C:\Users\admin\Desktop>
Run Code Online (Sandbox Code Playgroud)
为什么不工作,请帮帮我?