簡體   English   中英

用Java寫入.java文件並運行腳本

[英]Writing to a .java file in java and running the script

我目前正在嘗試用Java編寫.java文件。 我有一些類驅動程序,它調用我的子類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類運行此程序?

嘗試這個 :

 /**
 * @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

您可能沒有找到正確的地方。 您的代碼將寫入當前目錄的文件Auto.java 當前目錄是執行Java命令的目錄:

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

在上面的示例中,該文件將被寫入/foo/bar/baz/Auto.java

如果從IDE運行,則IDE中的“運行配置”應該讓您知道(並更改)當前目錄。 通常,IDE默認將其設置為項目的根目錄。

您確實應該添加一些異常處理:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM