2012-08-30 2 views
0

자바에서 박쥐 파일을 실행할 수 있으며 java에 정보를 보낼 수도 있습니다.실행중인 박쥐 파일에서 진행 진행

아래의 bat 파일을 실행하는 코드입니다. 어떻게 든 입력 스트림을 사용할 수 있다고 생각합니다. 나는 또한 상태를 얻기 위해 읽을 수 있도록 박쥐 파일에 물건을 남겨 둘 수 있다고 생각하고있었습니다.

다른 사람이 나에게 기본 진행률 막대를 추가하는 방법에 대한 코드를 제공하기를 바랍니다. 데모와 예제를 오라클에서 읽었지만 박쥐 파일에서 진도를 얻지는 않습니다.

아래 코드를 업데이트했지만, 여전히 실행해야하며 실제 100 % 완료를 위해 줄 수가 필요합니다. 그러나, 내가 생각했던 것처럼 진전을 보이지는 않습니다. 그것은 실행하고 0 %에 머무르며, 나는 그것이 작업 관리자 때문에 여전히 실행 중임을 알 수 있습니다.

public class ProgressBar extends JPanel 
          implements ActionListener, 
            PropertyChangeListener { 

private JProgressBar progressBar; 
private JButton startButton; 
private JTextArea taskOutput; 
private Task task; 

class Task extends SwingWorker<Void, Void> { 
    /* 
    * Main task. Executed in background thread. 
    */ 
    @Override 
    public Void doInBackground() { 
     Random random = new Random(); 
     int progress = 0; 
     //Initialize progress property. 
     setProgress(0); 
     //Sleep for at least one second to simulate "startup". 
     try { 
      Thread.sleep(1000 + random.nextInt(2000)); 
     } catch (InterruptedException ignore) {} 
     // while (progress < 100) { 
      //Sleep for up to one second. 
      try { 
       Thread.sleep(random.nextInt(1000)); 
      } catch (InterruptedException ignore) {} 
      //Make random progress. 
      try { 
       String ls_str; 
       Process ls_proc = Runtime.getRuntime().exec("\\WMIRebuild.bat"); 
      //  get its output (your input) stream  
       DataInputStream ls_in = new DataInputStream( 
        ls_proc.getInputStream()); 
       try { 
       while ((ls_str = ls_in.readLine()) != null) { 
        System.out.println(ls_str); 
        progress++; 
        } 
       } catch (IOException e) { 
       System.exit(0); 
       } 
      } catch (IOException e1) { 
       System.err.println(e1); 
       System.exit(1); 
      } 
      setProgress(Math.min(progress, 100)); 
     // } 
     return null; 
    } 

    /* 
    * Executed in event dispatch thread 
    */ 
    public void done() { 
     Toolkit.getDefaultToolkit().beep(); 
     startButton.setEnabled(true); 
     taskOutput.append("Done!\n"); 
    } 
} 

public ProgressBar() { 
    super(new BorderLayout()); 

    //Create the demo's UI. 
    startButton = new JButton("Start"); 
    startButton.setActionCommand("start"); 
    startButton.addActionListener(this); 

    progressBar = new JProgressBar(0, 18); 
    progressBar.setValue(0); 

    //Call setStringPainted now so that the progress bar height 
    //stays the same whether or not the string is shown. 
    progressBar.setStringPainted(true); 

    taskOutput = new JTextArea(5, 20); 
    taskOutput.setMargin(new Insets(5,5,5,5)); 
    taskOutput.setEditable(false); 

    JPanel panel = new JPanel(); 
    panel.add(startButton); 
    panel.add(progressBar); 

    add(panel, BorderLayout.PAGE_START); 
    add(new JScrollPane(taskOutput), BorderLayout.CENTER); 
    setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); 
} 

/** 
* Invoked when the user presses the start button. 
*/ 
public void actionPerformed(ActionEvent evt) { 
    progressBar.setIndeterminate(true); 
    startButton.setEnabled(false); 
    //Instances of javax.swing.SwingWorker are not reusuable, so 
    //we create new instances as needed. 
    task = new Task(); 
    task.addPropertyChangeListener(this); 
    task.execute(); 
} 

/** 
* Invoked when task's progress property changes. 
*/ 
public void propertyChange(PropertyChangeEvent evt) { 
    if ("progress" == evt.getPropertyName()) { 
     int progress = (Integer) evt.getNewValue(); 
     progressBar.setIndeterminate(false); 
     progressBar.setValue(progress); 
     taskOutput.append(String.format(
        "Completed %d%% of task.\n", progress)); 
    } 
} 

/** 
* Create the GUI and show it. As with all GUI code, this must run 
* on the event-dispatching thread. 
*/ 
private static void createAndShowGUI() { 
    //Create and set up the window. 
    JFrame frame = new JFrame("ProgressBarDemo2"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

    //Create and set up the content pane. 
    JComponent newContentPane = new ProgressBar(); 
    newContentPane.setOpaque(true); //content panes must be opaque 
    frame.setContentPane(newContentPane); 

    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 
    //Schedule a job for the event-dispatching thread: 
    //creating and showing this application's GUI. 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 

}

답변

0

main() 방법에서 별도의 진행률 표시 줄의 프레임을 가지고. 자신의 스레드에서 실행해야합니다. 배치 파일 ls_in.readLine()에서 내용을 읽으면 다른 프레임의 진행률을 업데이트하십시오. 예를 들어 프레임에 addTick()이라는 메서드를 추가하여 진행률 막대에 다른 블록을 추가 할 수 있습니다.이 블록은 배치 파일에서 한 줄을 읽을 때마다 호출 할 수 있습니다.


매우 기초적인 예 : 공정에서 읽어

스레드 :

while ((ls_str = ls_in.readLine()) != null) { 
     System.out.println(ls_str); 
     progressBar.addTick(); 
    } 

진행 바, 자체 스레드에서 실행

String bar = "#"; 

    public void addTick() 
    { 
     if(bar.length() < maxBarLength) { 
      bar = bar + "#"; 
      drawBar(bar); 
     } 
    } 

    public void draw() 
    { 
     drawBar(bar); 
    } 

    public void paint() 
    { 
     // draw some stuff 
     draw(bar); 
    } 

나도 몰라, 그런 식으로. 물론 진행률 표시 줄은 이와 다를 것입니다.이 코드는 main() 메서드가 진행률 표시 줄과 상호 작용하는 방식을 알려주는 의사 코드입니다.

+0

예를 들어 주겠니? 나는 네가 무엇을 얻고 있는지 거의 볼 수있다. – jerhynsoen

+0

@jerhynsoen은 내 대답 –

+0

에 의사 코드를 추가 했으므로 추천 코드와 진행률 표시 줄을 기반으로 위 코드를 업데이트했습니다. 오라클에서 테스트 해 보았습니다. 그러나 내가 위에서 말했듯이, 그것은 현재의 진드기를 보여주지 않는다. – jerhynsoen