JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java SE 討論區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 关于Runtime.getruntime().exec("")的一个问题,请各位达人指教!
lonestar42





發文: 79
積分: 0
於 2007-08-27 15:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我运行如下命令
  String moveCommand="cmd /k move /y D:\\app1.class D:\\test\\doUpdate";
p1 = Runtime.getRuntime().exec(moveCommand);
p1.destroy();
运行完后要System.exit(0);
开始发现我设断点观察程序执行的都是对的,但是一使用运行就出错,
后来我发现move这个命令要过个2,3秒才会真正执行完成,但是每次没有执行完成的时候程序就退出,导致执行结果有问题.请问有什么方法检查出exec执行完成么?


reply to postreply to post
100%纯北京爷们
作者 Re:关于Runtime.getruntime().exec("")的一个问题,请各位达人指教! [Re:lonestar42]
pcboy





發文: 27
積分: 0
於 2007-08-27 17:12 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
錯誤訊息是甚麼 ?

cmd 用完整路徑看看


reply to postreply to post
作者 Re:关于Runtime.getruntime().exec("")的一个问题,请各位达人指教! [Re:lonestar42]
yupin





發文: 343
積分: 1
於 2007-08-27 21:45 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
p1.destroy(); 之前加個 p1.waitFor() 試試~

reply to postreply to post
養了一隻獵犬,結果......現在變成神豬了!
作者 Re:关于Runtime.getruntime().exec("")的一个问题,请各位达人指教! [Re:lonestar42]
lonestar42





發文: 79
積分: 0
於 2007-08-28 09:37 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
写写楼上两位,
但是waitFor()我试过了,貌似把那个线程挂起了,但是再也启动不起来,我只能手动把程序关掉....感觉waitFor()挂起后,需要从外部用某种方法唤醒才可以

使用Object的wait()方法会报"current thread not owner"的错误


reply to postreply to post
100%纯北京爷们
作者 Re:关于Runtime.getruntime().exec("")的一个问题,请各位达人指教! [Re:lonestar42]
ryanpai





發文: 47
積分: 0
於 2007-08-28 10:45 user profilesend a private message to usersend email to ryanpaireply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
        // moveFile
        java.io.File sourceFile = new java.io.File("d:\\app1.class");
        java.io.File targetFile = new java.io.File("d:\\test\\doUpdate\\app1.class");
        if(sourceFile.exists()) sourceFile.renameTo(targetFile);


reply to postreply to post
Ryan Pai
作者 Re:关于Runtime.getruntime().exec("")的一个问题,请各位达人指教! [Re:lonestar42]
lonestar42





發文: 79
積分: 0
於 2007-08-28 11:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢!这种方法我也想过,但是如果来的是一个Jar包的话,我需要解压然后再移动整个目录去覆盖整个目录,我觉得可能还是需要cmd命令.不知道java有没有提供解压jar包的方法,我之前看到过一个jarFile的类,不知道有没有相应的方法

lonestar42 edited on 2007-08-28 11:05
reply to postreply to post
100%纯北京爷们
作者 Re:关于Runtime.getruntime().exec("")的一个问题,请各位达人指教! [Re:lonestar42]
ryanpai





發文: 47
積分: 0
於 2007-08-28 11:35 user profilesend a private message to usersend email to ryanpaireply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * moveCommand.java
 *
 * Created on 2007年8月28日, 上午 10:36
 */
 
import java.io.*;
public class moveCommand {
    
    /** Creates a new instance of moveCommand */
    public moveCommand() {
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try{
            // execute batch file
            String[] cmd = new String[3];
            Runtime rt = Runtime.getRuntime();
            cmd[0] = "cmd.exe" ;
            cmd[1] = "/C" ;
            cmd[2] = "move  D:\\app1.class D:\\test\\doUpdate" ;
            Process proc = rt.exec(cmd);
            //any error message?
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
            // any output?
            StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
            // kick them off
            errorGobbler.start();
            outputGobbler.start();
            // any error???
            int exitVal = proc.waitFor();
            String executeStatus = "";
            if(exitVal==0) executeStatus = "success..";
            if(exitVal==1) executeStatus = "failure....";
            
            System.out.print(executeStatus);
            
            System.exit(0);
        }
        catch(Exception e){e.printStackTrace();}
    }
    
 
    
    private static class StreamGobbler extends Thread {
        InputStream is;
        String type;
        
        StreamGobbler(InputStream is, String type) {
            this.is = is;
            this.type = type;
        }
        
        public void run() {
            try {
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line=null;
                while ( (line = br.readLine()) != null)
                    System.out.println(type + ">" + line);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }  // end StreamGobbler
    
}
 


欸...變調的問題...變變變


reply to postreply to post
Ryan Pai
作者 Re:关于Runtime.getruntime().exec("")的一个问题,请各位达人指教! [Re:lonestar42]
lonestar42





發文: 79
積分: 0
於 2007-08-29 14:16 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感谢您给的答案,但有几点不太明白.
1.process.waitFor() 这个方法我也试过但会导致整个进程全部挂起,就是无限制的wait下去....但是用这个程序就会等到当移动文件完成时退出.
2.关于streamGobbler这个类的作用是什么?除了检查错误状态和流内容外是否是当proccess.waitFor()执行后程序挂起后通过这个线程在外部对他进行唤醒或者退出?


lonestar42 edited on 2007-08-29 14:50
reply to postreply to post
100%纯北京爷们
作者 Re:关于Runtime.getruntime().exec("")的一个问题,请各位达人指教! [Re:lonestar42]
lonestar42





發文: 79
積分: 0
於 2007-08-30 15:09 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
对于copy move什么的都没问题,但是我运行一个解压缩的程序还是会造成整个进程的死锁

cmd = "cmd /k \"C:\\Program Files\\WinRAR\\Rar.exe\" e -o+ d:\\test.rar d:\\test" ;

OUTPUT>
OUTPUT>RAR 3.40 版权 (C) 1993-2004 Alexander Roshal 8 九月 2004
OUTPUT>已注册给 a
OUTPUT>
OUTPUT>
OUTPUT>正在从 d:\test.rar 中解压
OUTPUT>
OUTPUT>正在解压 d:\test\test.jar  99% 完成
OUTPUT>全部成功
OUTPUT>
就死在这里,必须手动停止才可以


reply to postreply to post
100%纯北京爷们
» JWorld@TW »  Java SE 討論區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8