使用其他java文件运行java文件时出错

时间:2014-06-29 11:52:32

标签: java error-handling

我实际上是在Windows中使用另一个java文件来运行我的java文件....这是我的代码:

private static void printLines(String name, InputStream ins) throws Exception {
    String line = null;
    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 {
      String s=System.getProperty("user.dir");
      s="C:\\Users\\HP\\Downloads\\apache-tomcat-7.0.54-windows-x64\\apache-tomcat-7.0.54\\webapps\\Mazil4.0\\WEB-INF\\classes";
  File workingDir = new File(s);
  System.out.println(q);
  //new Foo().nonStaticMethod();
Process pro = Runtime.getRuntime().exec(command,null,workingDir);

printLines(command + " stdout:", pro.getInputStream());
printLines(command + " stderr:", pro.getErrorStream());
pro.waitFor();
System.out.println(command + " exitValue() " + pro.exitValue());


 }


  public static void mai(String[] args) {

      String[] credentials=new String[4];int k=0;
      for (String s: args) {
          System.out.println(s);
        credentials[k]=s;k++;
    if(k==4)
    break;
      }


    try {
      //runProcess("javac test2.java");
        //thread foo=new thread();
      runProcess("java mainclasses.emaildownload "+credentials[0]+" "+credentials[1]+" "+credentials[2]+" "+credentials[3]+" ");

    } catch (Exception e) {
      e.printStackTrace();
    }System.out.println("hI");
  }

我使用workingDir给出了类的位置...我的文件路径是:

C:\Users\HP\Downloads\apache-tomcat-7.0.54-windows-x64\apache-tomcat-7.0.54\webapps\Mazil4.0\WEB-INF\classes\mainclasses\emaildownload.class

包名是mainclasses.but它仍然给出错误:

could not find or load main class mainclasses.emaildownload
可能是什么原因?

2 个答案:

答案 0 :(得分:0)

在这一行:

public static void mai(String[] args) {

是一个' n'丢失。

更改为

public static void main(String[] args) {

答案 1 :(得分:0)

我相信命令应该如下:

java -cp <path-to-jar> <main-class>

提供:

  1. 你要加载的主要类:mainclasses.emaildownload (包mainclasses中的类emadownownload)
  2. 包含给定的jar的补丁 主要课程: C:\用户\ HP \下载\ Apache的Tomcat的7.0.54 - - 窗口64 \ Apache的Tomcat的7.0.54 \ web应用\ Mazil4.0 \ WEB-INF \类\
  3. 命令应如下所示:

    java -cp C:\Users\HP\Downloads\apache-tomcat-7.0.54-windows-x64\apache-tomcat-7.0.54\webapps\Mazil4.0\WEB-INF\classes\<jar-file> mainclasses.emaildownload
    

    或者您可以使用通配符:

    java -cp "C:\Users\HP\Downloads\apache-tomcat-7.0.54-windows-x64\apache-tomcat-7.0.54\webapps\Mazil4.0\WEB-INF\classes\*" mainclasses.emaildownload