问题描述
- 关于用Java调用dos,有些命令可以,有些就不行
-
我用swing做了一个窗体,可以输入dos命令,然后在文本域控件输出结果,像ping命令这些可以成功,但输入java.java -version这些就完全没反应
解决方案
看什么命令,java.java是不是在当前路径,要么添加到环境变量,要么带上完整路径。
解决方案二:
Java调用DOS命令
java调用Dos命令
java 调用DOS命令
解决方案三:
核心代码是这样
runtime.getruntime().exec(csd)//
csd
是获取的输入的命令字符串,
解决方案四:
package newMenu;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Font;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.JLayeredPane;
import javax.swing.JScrollPane;
public class newView extends JFrame {
private JTextField textField;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
newView frame = new newView();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public newView() {
setTitle(" Mamager");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 791, 502);
setLocationRelativeTo(null);
getContentPane().setLayout(null);
JLabel label = new JLabel("u8BF7u8F93u5165uFF1A");
label.setBounds(108, 28, 54, 15);
getContentPane().add(label);
textField = new JTextField();
textField.setColumns(10);
textField.setBounds(154, 25, 403, 21);
getContentPane().add(textField);
final JTextArea textArea = new JTextArea(100,15);
textArea.setBounds(53, 54, 682, 379);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(53, 54, 682, 379);
getContentPane().add(scrollPane);
scrollPane.setColumnHeaderView(textArea);
JButton button = new JButton("u786Eu5B9A");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String command=textField.getText();
Runtime r=Runtime.getRuntime();
Process p = null;
try {
p = r.exec("cmd /c"+command);
int ret = 1,res=0;
do{
ret = p.waitFor();
res++;
} while(ret == 1&&res<=200000);
System.out.println("ret="+ret);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream()));
StringBuffer sb=new StringBuffer();
String inline;
try {
while(null!=(inline=br.readLine())){
System.out.println(inline);
textArea.append(inline+"
");
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
button.setBounds(567, 24, 93, 23);
getContentPane().add(button);
JMenuBar menuBar = new JMenuBar();
menuBar.setSize(new Dimension(0, 25));
menuBar.setBackground(Color.WHITE);
menuBar.setAutoscrolls(true);
menuBar.setAlignmentY(Component.BOTTOM_ALIGNMENT);
setJMenuBar(menuBar);
JMenu mnMenu = new JMenu("menu1");
menuBar.add(mnMenu);
JMenuItem mntmItem = new JMenuItem("item1");
mntmItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
mnMenu.add(mntmItem);
JMenu mnMenu_1 = new JMenu("menu2");
mnMenu_1.setBackground(Color.WHITE);
menuBar.add(mnMenu_1);
JMenuItem mntmItem_1 = new JMenuItem("item2");
mnMenu_1.add(mntmItem_1);
}
}