这是我更新的代码。这是我在与您提供的Java类分开的类中单击的按钮。我知道它说的是Ping(无视我正在使用按钮进行测试),我看不到它们如何与您提供的Process P行代码相互引用。你怎么看?
JButton btnPingComputer = new JButton("PING");
btnPingComputer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String line;
BufferedWriter bw = null;
BufferedWriter writer =null;
try {
writer = new BufferedWriter(new FileWriter(tempFile));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String lineToRemove = "OU=Workstations";
String s = null;
Process p = null;
try {
p = Runtime.getRuntime().exec("cmd /c start c:\\computerQuery.bat computerName");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
p = Runtime.getRuntime().exec("c:\\computerQuery.bat");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuffer sbuffer = new StringBuffer(); // new trial
BufferedReader in = new BufferedReader(new InputStreamReader(p
.getInputStream()));
try {
while ((line = in.readLine()) != null) {
System.out.println(line);
textArea.append(line);
textArea.append(String.format(" %s%n", line));
String dn = "CN=FDCD111304,OU=Workstations,OU=SIM,OU=Accounts,DC=FL,DC=NET";
LdapName ldapName = new LdapName(dn);
String commonName = (String) ldapName.getRdn(ldapName.size() - 1).getValue();
System.out.println(commonName);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidNameException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally
{
try {
fw.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
像这样将参数添加到您的java程序中:
Process p = Runtime.getRuntime().exec("cmd /c start c:\\batFile.bat computerName");
这将传递parameter_to_pass
到批处理文件。
对于您的情况,此代码应该可以正常工作:
/*
This java program copies the value from a jTextField, adds it to a predifined value
and send it to command-line as a parameter. All these happens if you click the jButton
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class cmdJavaTest extends JFrame {
JTextField jTextField1 = new JTextField(20);
JButton jButton1 = new JButton("Click");
JLabel jLabel1 = new JLabel();
public cmdJavaTest() {
super("CmdJavaParameterPass");
getContentPane().setLayout(new FlowLayout());
getContentPane().add(jTextField1);
getContentPane().add(jButton1);
getContentPane().add(jLabel1);
jButton1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
sendParam();
}
});
setSize(300, 170);
setVisible(true);
}
public void sendParam(){
try{
String val = "Computer"+jTextField1.getText(); //Put whatever you want to pass as a prefix in place of "Computer"
jLabel1.setText(val);
Process p ;
p = Runtime.getRuntime().exec("cmd /c start c:\\batFile.bat "+val+"");
}
catch(Exception e){
e.printStackTrace();
}
}
public static void main(String argv[]) {
new cmdJavaTest();
}
}
在测试批处理文件内容时使用它
@dsquery computer -name %1 pause
但是,您还必须了解如何使用ProcessBuilder
。
谢谢,希望对您有所帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句