Meine Java Programm hägt durch waitFor() ! HILFE!

Status
Nicht offen für weitere Antworten.

Magic-Alex

Mitglied
Hallo allerseits!

Ich schreibe zur Zeit an einer kleinen Anwedung, welche unter anderem mir auch eine Diskette
formatieren soll. Dazu rufe ich per runtime... den Dos Format Befehl inkl. Parameter auf. Daraufhin
mache ich ein waitFor(), damit mein Programm wartet bis die Formatierung beendet ist, und erst
danach sollen Dateien auf die Diskette geschrieben werden.

Nun mein Problem:
Mein Programm hat eine kleine mit Swing geschriebene Oberfläche
bekommen. Leider tut sich da während er wartet bis die Formatierung beendet ist gar nichts. Wäre
ganz schon wenn ich wenigstens in meiner jTextArea was angezeigt bekommen würde.

Code:
private void Format_actionPerformed(ActionEvent e) throws IOException
{
	jTextArea.setText("");
	jTextArea.append("Bitte warten bis Formatierung der Diskette beendet ist");
	try {
	    Process form = Runtime.getRuntime().exec
                                  ("command.com /C start ECHO J | format A: /FS:FAT /V:Resotec /F:720 /X");
		try {
			form.waitFor();
		} catch (InterruptedException e2) {e2.printStackTrace();}
	} catch (IOException e1) {e1.printStackTrace();}

	jTextArea.append("Dateisystem wird auf Diskette erstellt");
		
	for (int i=0; i<=9; i++){	
		File fSrc = new File("D:\\Files\\DA0"+i);
		File fDes = new File("a:\\DA0"+i);
		FileInputStream fis = new FileInputStream (fSrc);
		FileOutputStream fos = new FileOutputStream (fDes);
		
		byte buf[] = new byte[8192];
		while(fis.read(buf)!=-1){
			fos.write(buf);			
		}
		fis.close();
		fos.flush();
		fos.close();
		}		
}

Alles was in der TextArea angezeigt werden soll, schreibt er erst nach der kompletten Abarbeitung rein.
Wäre schön wenn er auch mir am Anfang wenigstens schreibt:
("Bitte warten bis Formatierung der Diskette beendet ist")

Hoffe Ihr könnt mir weiterhelfen. Vielen dank auf jeden fall schon einmal. MFG Alex
 
Erkundige dich was "Threads" sein, denn solch einer benötigst du.

Ein Beispiel gibts in unserer FAQ.
 
So wie es aussieht setzt du das waitFor im EventThread ab, und der ist dann blockiert bis das formatieren abgeschlossen ist.
 
Danke für die schnellen Antworten...das hab ich mir auch schon fast so gedacht. Nur ist es für mich auch wichtig, das meine Anwedung erst mit dem schreiben beginnt, wenn das formatieren abgeschlossen ist.

Wie kann ich nun umgehen, dass die ganze Anwenung gesperrt bleibt?

Alex
 
dein problem ist das format auf stderr oder stdout schreibt. da diese streams nie ausgelesen werden, beendet sich der prozess nie. also alles in einen thread packen und aout+err stream auslesen
 
noch was zu wait und waitFor. IMMER innerhalb einer Schleife aufrufen !

Testing the condition before waiting and skipping the wait if the condition already holds are necessary to ensure liveness. If the condiftion already holds and notify (notfiyAll) method has already been invoked before a thread waits, there is no guarantee that the thread will ever waken from the wait.
Testing the condition after waiting and waiting again if the condition does not hold are necessary to ensure safety. If the thread proceeds with the action when the condition does not hold, it can destroy the invariants protected by the lock.....
 
genau, damit man sich beim consumer/producer pattern auch fein in den fuss schiesst (gut, mit der 1.5 macht man es eh anders)
 
ungefähr so:

Code:
class ProcessRunner implements Runnable{
   private String cmd;
   public ProcessRunner(String cmd){
      this.cmd = cmd;
   }
   public void run(){
      Process p = Runtime.getRuntime().exec(cmd);
      p.waitFor();
      this.interrupt();
   }
}

Im Hauptprogramm dann:

Code:
Thread processRunner = new Thread(new ProcessRunner("cmd befehl"));
processRunner.start();

while(!processRunner.isInterrupted()){
   //status in textarea
}
 
und ebend das ist nicht deterministisch
vgl http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
section Why Runtime.exec() hangs
liest hier garniemand die vorposts?

besonders fein
"Is this just a case of programmers not reading the documentation, as implied in the oft-quoted advice: read the fine manual (RTFM)? The answer is partially yes. In this case, reading the Javadoc would get you halfway there; it explains that you need to handle the streams to your external process, but it does not tell you how."
 
Verstehe ich das nun richtig, auch wenn ein externe Process
nur Daten ins Dateien schreibt, muss ich die stdout und stderr Ströme behandeln?
Würde damit folgendes Behandlung ausreichen?

<code>
...
Process p = Runtime.exex("xxx.exe");
p.getgetErrorStream().close();
p.getInputStream().close();
p.waitFor();
...
</code>

Danke schonmal 😉
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben