0

我目前正在尝试用 Java 编写一个 .java 文件。我有一些类 Driver 调用我的子类 Eval:

public class Driver
{
    public static void main(String[] args)
    {
        Eval eval = new Eval();
        eval.evaluate("System.out.println(\"Hello!!! I'm an auto-generated .java file!);\");
    }
}

这是我的 Eval 类(存储在同一个项目中):

public class Eval
{
    public Eval()
    {

    }

    public void evaluate(String toEval)
    {
        PrintWriter writer = new PrintWriter("Auto.java", "UTF-8");
        writer.println("public class Auto");
        writer.println("{");
        writer.println("    public static void main(String[] args)");
        writer.println("    {");
        writer.println("        " + toEval;
        writer.println("    }");
        writer.println("}");
        writer.close();
    }
}

因此,我在同一目录中手动创建了 Auto.java 类。我运行脚本,查看 Auto.java,令我惊讶的是,什么都没写!

public Auto
{
}

(这是我在手动创建它后留下的方式)
任何建议将不胜感激。
谢谢!

PS 此外(尽管上述错误优先于这个问题),我如何从 Eval 类运行这个程序?

4

3 回答 3

2

尝试这个 :

 /**
 * @author Rustam
 */
public class Test {


    public static void main(String[] args) throws FileNotFoundException,
            UnsupportedEncodingException {

        Eval eval = new Eval();
        eval.evaluate("System.out.println(\"Hello!!! I'm an auto-generated .java file!\")");
        try {
            runProcess("javac Auto.java");
            runProcess("java Auto");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    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());
    }

    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);
        }
    }
}

class Eval
{
    public Eval()
    {

    }

    public void evaluate(String toEval) throws FileNotFoundException, UnsupportedEncodingException
    {
        PrintWriter writer = new PrintWriter("Auto.java", "UTF-8");
        writer.println("public class Auto");
        writer.println("{");
        writer.println("    public static void main(String[] args)");
        writer.println("    {");
        writer.println("        " + toEval + ";");
        writer.println("    }");
        writer.println("}");
        writer.close();
        System.out.println("Auto.java created..");
    }
}

输出 :

Auto.java created..
javac Auto.java exitValue() 0
java Auto stdout: Hello!!! I'm an auto-generated .java file!
java Auto exitValue() 0
于 2014-10-24T16:40:10.437 回答
1

你可能没有找对地方。您的代码写入当前目录Auto.java的文件中。当前目录是执行 java 命令的目录:

> pwd
> /foo/bar/baz
>
> java com.mycompany.Driver

在上面的示例中,文件将被写入/foo/bar/baz/Auto.java.

如果您从 IDE 运行,那么 IDE 中的“运行配置”应该让您知道(并更改)当前目录。它通常由 IDE 默认设置为项目的根目录。

于 2014-10-24T16:18:30.913 回答
0

你真的应该添加一些异常处理:

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

public class Driver
{
    public static void main(String[] args) throws FileNotFoundException,  UnsupportedEncodingException
    {
        Eval eval = new Eval();
        eval.evaluate("System.out.println(\"Hello!!! I'm an auto-generated .java file!);\");");

       //here we create an instance of the class (which will be created with code above) Auto
       Auto auto = new Auto();
    }
}

然后你还需要在你的主类中添加一些异常处理:

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
public class Eval
{
    public void evaluate(String toEval) throws FileNotFoundException, UnsupportedEncodingException
    {
        PrintWriter writer = new PrintWriter("Auto.java", "UTF-8");
        writer.println("public class Auto");
        writer.println("{");
        writer.println("    Auto(){"); //You don't need another public static... just a constructor that prints the message
        writer.println("        " + toEval);
        writer.println("    }");
        writer.println("}");
        writer.close();
    }
}

现在我已经在上面测试了它并且它可以工作,所以请确保您查看正确的目录并确保文件 Auto 位于正确的位置(您可能需要指定路径),即:

PrintWriter writer = new PrintWriter("C:/Users/You/Desktop/..../test.txt", "UTF-8");

于 2014-10-24T16:31:43.393 回答