0

我在 netbeans 项目的一个包中有 2 个 java 程序。1个是Run.java,另一个是JdbcSqlServerConnectivity.java。我提到了关于这个主题的早期问题,但似乎没有一个解决方案有效。我收到以下错误

javac JdbcSqlServerConnectivity.java stderr: javac: file not found: JdbcSqlServerConnectivity.java
javac JdbcSqlServerConnectivity.java stderr: Usage: javac <options> <source files>
javac JdbcSqlServerConnectivity.java stderr: use -help for a list of possible options
javac JdbcSqlServerConnectivity.java exitValue() 2
java JdbcSqlServerConnectivity stderr: Error: Could not find or load main class JdbcSqlServerConnectivity
java JdbcSqlServerConnectivity exitValue() 1

运行.java代码

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Run {

  private static void printLines(String name, InputStream ins) throws Exception {
    String line;
    BufferedReader in = new BufferedReader(
        new InputStreamReader(ins));
    while ((line = in.readLine()) != null) {
        System.out.println(name + " " + line);
    }
  }

  private static void runProcess(String command) throws Exception {
    Process pro = Runtime.getRuntime().exec("/" + command + "");
    printLines(command + " stdout:", pro.getInputStream());
    printLines(command + " stderr:", pro.getErrorStream());
    pro.waitFor();
    System.out.println(command + " exitValue() " + pro.exitValue());
  }

  public static void main(String[] args) {
    try {
      runProcess("javac JdbcSqlServerConnectivity.java");
      runProcess("java JdbcSqlServerConnectivity");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

提前致谢

4

2 回答 2

0

您应该提供 file 的完整系统路径JdbcSqlServerConnectivity.java。例如,如果JdbcSqlServerConnectivity.java位于D:\Temp\com\example目录内,则应指定如下:

runProcess("javac D:\\Temp\\com\\example\\JdbcSqlServerConnectivity.java");
runProcess("java D:\\Temp\\com\\example\\JdbcSqlServerConnectivity");
于 2013-03-07T10:17:22.267 回答
0

尝试这个:

runProcess("java -cp全路径 JdbcSqlServerConnectivity.java ");

于 2015-05-25T10:23:00.143 回答