2015-06-13 48 views
0

這裏是我更新的代碼。這是在我單獨從您提供的java類中單獨創建的類中單擊的按鈕。我知道它說平(無視,我使用按鈕進行測試)我不明白他們將如何與您提供的Process P代碼行相互引用。你怎麼看?將java變量添加到批處理文件

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(); 

       } 

      } 

     }); 
+0

您使用參數調用批處理文件。這不是你上次問這個問題的重複嗎? –

回答

2

的參數添加到您的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

謝謝,希望它有幫助

+0

評論不適合長時間討論;這個對話已經[轉移到聊天](http://chat.stackoverflow.com/rooms/80942/discussion-on-answer-by-mustangdc-adding-java-variable-to-batch-file)。 –