通过批处理文件运行java代码& asp.net c#抛出异常

时间:2014-03-17 04:10:52

标签: c# java asp.net batch-file

我从' .bat file throws exception at Line br.readLine()`执行java程序时遇到错误。当手动执行批处理文件时,它执行正常

Exception in thread "main" java.io.IOException: The handle is invalid
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at java.io.BufferedInputStream.read1(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at test9.main(test9.java:13)

bat文件代码:

@echo off
if exist "C:\c\jop.txt" (
del "C:\c\jop.txt"
if exist "C:\c\test9.class" (
del "C:\c\test9.class"
)
) 

set path=%PATH%;C:\Program Files\Java\jdk1.6.0\bin
cd C:\c
javac test9.java >> jop.txt 2>&1
echo Successfully Compiled
java test9 >> jop.txt 2>&1

示例Java程序:

import java.io.*;
public class test9
{ 
   public static void main(String[] args) throws Exception
   {
      BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
      int a;
      float b;
      String s;
      System.out.println("Enter a string");
      s = br.readLine();
      System.out.println("You entered string "+s);
   }
}

它的s = br.readLine();给了我例外

C#代码:(在asp.net中使用)

string str = @"C:\c\javacompiler.bat";
Process myProcess = new Process();
myProcess.StartInfo.FileName = str;
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(str);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();       
myProcess.WaitForExit();
myProcess.Close();
TextBox2.Text = File.ReadAllText(@"C:\c\jop.txt");

0 个答案:

没有答案