易截截图软件、单文件、免安装、纯绿色、仅160KB

Java调用系统命令学习(三)

学习了两篇的Runtime类,现在对它有了更深一层的了解,那么我们来看看下面的代码:
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader ;
import java.io.BufferedReader;
public class Exec_Output{
public static void main(String []args)throws IOException,InterruptedException{
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("dir");
//int exitValue = p.exitValue();
//int exitValue = p.waitFor();
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<OUTPUT>");
while((line = br.readLine())!=null){
System.out.println(line);
System.out.println("</OUTPUT>");
int exitValue = p.waitFor();
System.out.println("Process exitValue="+exitValue);
}
}
}
//执行结果(在Ubuntu9.10下执行)
<OUTPUT>
class CUtil.java Exec.java Exec_Javac.java Exec_Output.java Str.java
</OUTPUT>

因为以上代码,我使用了Ubuntu9.10下执行,是一点问题都没有,但当我在windows xp下执行,意外却发生了。
E:classescomjavaworldjpitfallsarticle2>java BadExecWinDir
java.io.IOException: CreateProcess: dir error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Unknown Source)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at BadExecWinDir.main(BadExecWinDir.java:12)
也许大家觉得奇怪,其实我也很奇怪,为什么呢。好好在Linux下,咋到XP就有问题了呢。
以下引用了别人的话:
说实


相关文档:

JAVA将内容追加到文件尾部

import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
 * 将内容追加到文件尾部
 */
public class AppendToFile
{
    /**
     * A方法追加文件:使用RandomAccessFile
     *
    ......

Java调用SQL Server的存储过程详解

1使用不带参数的存储过程
  使用 JDBC 驱动程序调用不带参数的存储过程时,必须使用 call SQL 转义序列。不带参数的 call 转义序列的语法如下所示:
  
以下是引用片段:
{call procedure-name}
  作为实例,在 SQL Server 2005 AdventureWorks 示例数据库中创建以下存储过程:
  
以下是引用片段: ......

java php DES 加密解密

import java.io.IOException;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DES {
private byte ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号