简体   繁体   English

如何使用Java程序保存和打开Java程序?

[英]How do you save and open a Java program with a Java program?

So I've done a little searching and Google and the StackOverflow search brings up nothing regarding this question. 所以我做了一点搜索,谷歌和StackOverflow搜索没有提出这个问题。 I thought I had found something a moment ago but it just wanted to know how to open a .exe with Java. 我以为我刚才找到了什么,但它只是想知道如何用Java打开.exe。

Anyway as the title says I would like to know how to compile and save a Java file from within a Java program and then open that same Java file I just saved. 无论如何,标题说我想知道如何从Java程序中编译和保存Java文件,然后打开我刚刚保存的同一个Java文件。

So yeah any help with this would be greatly appreciated thanks. 所以,对此任何帮助都将非常感谢谢谢。

EDIT:Okay what I'm talking about is to have the Java file execute. 编辑:好的,我所说的是让Java文件执行。

The idea I have is using a genetic algorithm as the base and a whole lot of learning I have to yet do. 我的想法是使用遗传算法作为基础和我必须做的大量学习。 Program a program that can create programs within given parameters. 编写可以在给定参数内创建程序的程序。 That would then launch itself and continue the cycle of improving itself. 然后这将自我发起并继续改善自身的循环。 Basically self improving code. 基本上是自我改进的代码。 At the moment I have the base code of the genetic algorithm and am just trying to learn how it works as it was a tutorial I found. 目前我有遗传算法的基本代码,我只是想了解它是如何工作的,因为它是我发现的教程。 Yes I know it may not be possible but I like the challenge and it gives me a goal and a in my opinion fun reason to learn Java as I have attempted to before but I didn't get very far. 是的,我知道这可能是不可能的,但我喜欢这个挑战,它给了我一个目标,并且在我看来有趣的理由学习Java,就像我之前尝试过的那样,但我并没有走得太远。

Anyway sorry for any confusion. 不管怎么说抱歉有任何困惑。

Edit2: So this is basically the code being used it has been slightly modified and messed around with though at the moment to see what I can get it to do already: http://www.theprojectspot.com/tutorial-post/creating-a-genetic-algorithm-for-beginners/3 编辑2:所以这基本上是使用它的代码稍微修改和混乱,虽然目前看到我可以得到它做什么: http//www.theprojectspot.com/tutorial-post/creating-一个遗传算法换初学者/ 3

After your edit, I understood that your question is: 编辑后,我明白你的问题是:

How can I write a java program that is clever enough to create more clever java programs? 我怎样才能编写一个足够聪明的java程序来创建更聪明的java程序?

It is going to be a difficult task. 这将是一项艰巨的任务。 If you have the solution for the artificial intelligence that is necessary for this, then you need a technical solution for how to dynamically create a new class in runtime. 如果您拥有必要的人工智能解决方案,那么您需要一个技术解决方案来了解如何在运行时动态创建新类。 I can suggest two options for that. 我可以为此提出两个选择。

  1. Use the java compiler API 使用java编译器API
  2. Or use the javassist library. 或者使用javassist库。

You can use http://commons.apache.org/proper/commons-exec/ to start processes from Java code. 您可以使用http://commons.apache.org/proper/commons-exec/从Java代码启动进程。 You should call the same things as you would form command line. 你应该调用与命令行相同的东西。 ( javac -cp... and then java -cp.. someclass ) javac -cp...然后java -cp.. someclass

import java.io.*;

public class MyProg 
{
    public static void main(String[] args) 
        throws IOException, InterruptedException
    {
        String fname = "Prog1.java";
        BufferedWriter bw;

        try{
            bw = new BufferedWriter(
                    new FileWriter(fname)
            );
        } 
        catch (IOException e) {
            System.out.println("Cannot open " + fname + "!");
            return;
        }


        final String NL = System.getProperty("line.separator");

        bw.write("public class Prog1{" + NL
                + "\tpublic static void main(String[] args) {" + NL
                + "\t\tSystem.out.println(\"hello world\");" + NL
                + "\t}" + NL
                + "}" + NL
        );
        bw.close();

        BufferedReader br;
        try {
            br = new BufferedReader(
                    new FileReader(fname)
            );
        }
        catch (FileNotFoundException e) {
            System.out.println(fname + " not found!");
            return;
        }

        String line;
        while( (line = br.readLine()) != null )
        {
            System.out.println(line);
        }





        Process p1 = new ProcessBuilder(
            "javac", "Prog1.java"
        ).start();

        int error = p1.waitFor();
        System.out.println("p1: " + error);

        /*
        Process p2 = new ProcessBuilder(
            "java", "-cp . Prog1"
        ).start();

        error = p2.waitFor();

        System.out.println("p2: " + error);
        */

        FileInputStream fin;
        try {
            fin = new FileInputStream(fname);
        }
        catch (FileNotFoundException e) {
            System.out.println(fname + " not found!");
            return;
        }

        int abyte;
        while((abyte = fin.read()) != -1)
        {
            System.out.println(abyte);
        }

        fin.close();


    }
} 


--output:--
public class Prog1{
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}
p1: 0
112
117
98
108
105
99
...
...
125
10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM