如何在Java程序中运行Java源代码 [英] How to run Java source code within a Java program

查看:371
本文介绍了如何在Java程序中运行Java源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一些代码来编译Java源代码。然后生成.class文件。问题是我如何运行它?



例如,我确定的程序和类的设置的名称,我使用prog p = new prog (),在这种情况下,类文件不存在,直到我编译它。真的不知道该怎么办。有人可以给我一个建议吗?



btw,类看起来像这样:

  public void compile {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null,null,null,fileToCompile);
}

public void run(){
Prog prog = new Prog();
prog.run();
}


解决方案

它可以启动一个java进程,使用
Runtime.exec ProcessBuilder 。这些将创建一个独立的java进程来运行你的java程序。这更可能是你想要的。你基本上可以做如下等效的:

 > java someClass 

此链接可能有所帮助。



如果您想实际加载类文件并在当前应用程序中使用它,我认为 this ,或动态加载Java类应该有帮助。基本上(直接从链接,稍作修改):

  public class MainClass {

public static void main(String [] args){

ClassLoader classLoader = MainClass.class.getClassLoader();

try {
Class aClass = classLoader.loadClass(MyClass);
System.out.println(aClass.getName()=+ aClass.getName());
} catch(ClassNotFoundException e){
e.printStackTrace();
}

}

加载类后,拥有 Class 对象,而您可以通过调用 aClass.newInstance()创建由 aClass 表示的类的实例, >

MyClass newObj = new MyClass()



使用 Class 对象公开的任何其他方法。



如davmac指出的,上面的代码示例假定你加载的代码在你的应用程序类路径上。如果您要运行的类文件不在您的类路径中,您可能需要查看 URLClassLoader


I have wrote some code to compile a Java source code. It then produces the .class file. The problem is how do I run it?

For example, I am ok with the name of the program and class being set, I've used prog p = new prog(), in this case, however, the class file does not yet exist until I compile it. Not really sure what to do. Can someone give me an advice?

btw, the class looks like this:

public void compile{
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();  
  int compilationResult = compiler.run(null, null, null, fileToCompile);  
}

public void run(){
  Prog prog = new Prog();
  prog.run();
}

解决方案

If you just want to run it, you could launch a java process using Runtime.exec or ProcessBuilder. These will create a seperate java process to run your java program. This is more likely what you want. You can essentially do the equivelant of:

>java someClass

from within your application. This link may help.

If you want to actually load the classfile and use it in your current application, I think something along the lines of this, or dynamically loading Java Classes ought to help. Basically (directly from the link, slightly modified):

public class MainClass {

  public static void main(String[] args){

    ClassLoader classLoader = MainClass.class.getClassLoader();

    try {
        Class aClass = classLoader.loadClass("MyClass");
        System.out.println("aClass.getName() = " + aClass.getName());
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

Once you loaded the class, you have a Class object, and you can create an instance of the class represented by aClass by calling aClass.newInstance(), which is like

MyClass newObj = new MyClass()

Or you can use any of the other methods the Class object exposes.

As pointed out by davmac, the code sample above presumes that the code you're loading is on your applications classpath. If the class files you want to run are not in your classpath, you might want to look into URLClassLoader

这篇关于如何在Java程序中运行Java源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆