jdk中带有日志api不知道是不是干这个的
没用过

解决方案 »

  1.   

    我也没用过 我是刚学java的我试了在dos下的输出重定向 不好使请帮我写写大家了
      

  2.   

    用Runtime与Process吧!
    import java.io.*;public class   JavaCP
    {
    public static void  main(String[] args)
    {
    try
    {
    Runtime rt=Runtime.getRuntime(); //Process pr=rt.exec("C:\\j2sdk1403\\bin\\javac.exe  -classpath  .;d:\\Java; TestJavaCP.java");//指明绝对路径
    Process pr=rt.exec("java TestJavaCP");//与上面效果一样
    //Process pr=rt.exec("javac  -classpath  .;d:\\Java; TestJavaCP.java");//java文件必须放置在当前目录。
    //Process pr=rt.exec("java  -version"); //pr.waitFor();//如想得到输出结果,最好不要用该方法。 //OutputStream getOutputStream()
    //InputStream  is=pr.getErrorStream();
    InputStream  is=pr.getInputStream();
    BufferedReader  br=new BufferedReader(new InputStreamReader(is));
    String tmp=null;
    while((tmp=br.readLine())!=null)
    {
    System.out.println(tmp);
    }
    }
    catch(Exception err)
    {
    System.out.println(err.getMessage());
    }
    }
    }
      

  3.   

    我是想取到javac的编译信息 你的方法不行
    但用在
    别的程序好使
      

  4.   

    什么叫取得javac的编译信息?
    是指在运行javac时在控制台下打印的报错信息吗?
      

  5.   

    是呀 如:编译时 或 java -help
    看到的信息
      

  6.   

    用delphi实现也行的
    最好用java和delphi都能实现
      

  7.   

    果然不好用
    看来只有等着Leemaasn(我给大家拜早年啦!新春快乐!!!)大哥解决了 ^_^
      

  8.   

    java -help 的信息我倒有办法输出,编译信息就没办法了java -help | more > info.txt
      

  9.   

    实现我不会,但也许可以按这个思路:
    想办法把dos窗口的所有字符都输出到文件;
    (这个用手动复制粘贴我想大家都会,实现模拟鼠标键盘的操作我想也不会太难)
    然后你再对这个文件操作,去掉没用的信息,不就行了?
      

  10.   

    很感谢Eraserpro(萍水相逢是缘,飞来横祸是命)你的办法可以看到帮助
    谢谢
    但我的目的是编译信息
    还要请大家继续想办法
      

  11.   

    在linux下就简单多了。一个&>搞定
      

  12.   

    //output information to text file.
    //I don't know why information will generated through ErrorStream, as the
    // subprocess executed sucessfully. Any Explaination?
    // Anyway following code works.public static void main(String args[]){
       try{            
          Runtime rt = Runtime.getRuntime();
          Process proc = rt.exec("javac -verbose Test.java");
          
          InputStream stdin = proc.getErrorStream(); //I don't why only ErrorStream
          InputStreamReader isr = new InputStreamReader(stdin);
          BufferedReader br = new BufferedReader(isr);
          FileWriter wr =new FileWriter(new File("1.txt"));
          System.out.println("<OUTPUT to 1.txt>");
          String line;
          while ( (line = br.readLine()) != null)
              wr.write(line+"\r\n");
          wr.flush();
          System.out.println("</OUTPUT>");
          //close stream ....
          int exitVal = proc.waitFor();            
          System.out.println("Process exitValue: " + exitVal);     
          } catch (Throwable t){
                t.printStackTrace();
          }
    }
      

  13.   

    编译信息?随便定义的名字把,你编译的时侯如果不出错那么就没有编译的信息了,出错的
    信息当然可以保存到.txt文件里面阿  重定向
      

  14.   

    public class TestError
    {
    public static void main(String[] args)
    {
    System.djkf;
    }
    }
    这个文件是有错误的,你在dos下面用javac TestError.java 2> error.txt编译的时候
    控制台并不显示错误 但是在这个目录下就会有一个error.txt文件 内容是
    TestError.java:5: not a statement
    System.djkf;
                          ^
    1 error
    这个前提是你得系统为NT/2000/xp
    如果你得系统是85/98的话不支持重定向功能,要么省级操作系统 要么就下一个errout程序
    errout javac TestError.java > error.txt
    建议省级操作系统 98不行  烂
      

  15.   

    建议你用我的方法重新封装javac.exe和java.exe,编译时直接使用你自己的方法。JCreater与TextPad的编译共能的实现原理与此类似。
      

  16.   

    使用 UltraEdit 和 JDK 联合编程的话,就可以把输出信息保存在一个文本文件里面。
      

  17.   

    UltraEdit就可以啊!按F9输入javac j.java,编译信息就会自动显示到一个新编辑窗口了.
      

  18.   

    mingjava(学习) 
    说的好像对大家帮着看看为什莫重定向加个2你在dos下面用javac TestError.java 2> error.txt编译
      

  19.   

    louisqiang(tenwin) 和 wobelisk() 的解决方法正确的CSDN是个初级选手云集的地方
    仅仅指个方向,不会认为你是正确的象两位高手一样的高手
    如果用更多的时间去指方向
    而不是详细的编码,将做多么多有意义的事呜呼,唉哉~~~CSDN不开高级版,高手们何去何从??
      

  20.   

    InputStream stdin = proc.getErrorStream(); //I don't why only ErrorStreamme too
      

  21.   

    谢谢大家 我弄懂了
    InputStream stdin = proc.getErrorStream(); //I don't why only ErrorStream是取得错误信息的关键
    给分了
      

  22.   

    Leemaasn :我昨天调休,没看到,不好意思。
    我是在别的程序中通过创建管道(pipe)实现输出重定向来获得编译后的输出信息的
      

  23.   

    呵呵 
    来晚了,其实我对java并不是很熟,只不过为了响应Leemaasn兄弟的需要,写过一个方便编译的小工具,才想办法实现这个功能的
      

  24.   

    我是把要执行的命令写入一个批处理文件,然后构建一个类,在类中执行这个批处理文件并接收输出,用vb作的,挺简单的,有一点vb的基础就应该能看懂下面这个函数,这是类中的主要的函数,基本上只要这个函数就可以了
    Public Function ExecuteCommand(Optional CommandLine As String) As String
        Dim proc As PROCESS_INFORMATION     'Process info filled by CreateProcessA
        Dim ret As Long                     'long variable for get the return value of the
                                            'API functions
        Dim start As STARTUPINFO            'StartUp Info passed to the CreateProceeeA
                                            'function
        Dim sa As SECURITY_ATTRIBUTES       'Security Attributes passeed to the
                                            'CreateProcessA function
        Dim hReadPipe As Long               'Read Pipe handle created by CreatePipe
        Dim hWritePipe As Long              'Write Pite handle created by CreatePipe
        Dim lngBytesread As Long            'Amount of byte read from the Read Pipe handle
        Dim strBuff As String * 256         'String buffer reading the Pipe    'if the parameter is not empty update the CommandLine property
        If Len(CommandLine) > 0 Then
            mCommand = CommandLine
        End If
        
        'if the command line is empty then exit whit a error message
        If Len(mCommand) = 0 Then
            MsgBox "Command Line empty", vbCritical
            Exit Function
        End If
        
        'Create the Pipe
        sa.nLength = Len(sa)
        sa.bInheritHandle = 1&
        sa.lpSecurityDescriptor = 0&
        ret = CreatePipe(hReadPipe, hWritePipe, sa, 0)
        
        If ret = 0 Then
            'If an error occur during the Pipe creation exit
            MsgBox "CreatePipe failed. Error: " & Err.LastDllError, vbCritical
            Exit Function
        End If
        
        'Launch the command line application
        start.cb = Len(start)
        start.dwFlags = STARTF_USESTDHANDLES Or STARTF_USESHOWWINDOW
        'set the StdOutput and the StdError output to the same Write Pipe handle
        start.hStdOutput = hWritePipe
        start.hStdError = hWritePipe
        'Execute the command
        ret& = CreateProcessA(0&, mCommand, sa, sa, 1&, _
            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
            
        If ret <> 1 Then
            'if the command is not found ....
            MsgBox "File or command not found", vbCritical
            Exit Function
        End If
        
        'Now We can ... must close the hWritePipe
        ret = CloseHandle(hWritePipe)
        mOutputs = ""
        
        'Read the ReadPipe handle
        Do
            ret = ReadFile(hReadPipe, strBuff, 256, lngBytesread, 0&)
            mOutputs = mOutputs & Left(strBuff, lngBytesread)
            'Send data to the object via ReceiveOutputs event
            RaiseEvent ReceiveOutputs(Left(strBuff, lngBytesread))
        Loop While ret <> 0
        
        'Close the opened handles
        ret = CloseHandle(proc.hProcess)
        ret = CloseHandle(proc.hThread)
        ret = CloseHandle(hReadPipe)
        If mOutputs <> "" Then
            blnCompiledSuccessful = False
            Open strRoot & "BatchBuild.log" For Append As #3
            Print #3, ""
            Print #3, mOutputs
            'Print #3, ""
            Print #3, "-------------------------------------------------------------------------------------------------"
            Close #3
        Else
            blnCompiledSuccessful = True
            Open strRoot & "BatchBuild.log" For Append As #3
            Print #3, ""
            Print #3, strTargetFile & "        编译成功"
            Print #3, ""
            Print #3, "-------------------------------------------------------------------------------------------------"
            Close #3
        End If
        'Return the Outputs property with the entire DOS output
        ExecuteCommand = mOutputs
    End Function
      

  25.   

    呵呵。
    aikill(大傻)果然够兄弟!!!!!!!!!!!!!!!!!!!!!!!!
    楼主看看吧,有问题再提吧!
      

  26.   

    javac test.java 1>Err.txt 2>&1