简体   繁体   English

从现有的Java程序编译并创建新的Java程序的实例

[英]Compile and create instance of a new java program from existing java program

I am writing a java program in eclipse to extract forms from lotusnotes db. 我正在用eclipse编写Java程序,以从Lotusnotes db中提取表单。 Whenever I get a form I need to create a .java file containng a class with same name as of form. 每当我收到表单时,都需要创建一个.java文件,其中包含与表单名称相同的类。 Then I have to compile this new .java class which gives me .class file & hence I can create an instance of that new class. 然后,我必须编译这个新的.java类,这会给我.class文件,因此我可以创建该新类的实例。 I need to know whether this approach of creating,compiling & instantiating a new class is possible from a single existing java program. 我需要知道从单个现有的Java程序是否可以使用这种创建,编译和实例化新类的方法。

My pseudocode goes below 我的伪代码如下

import java.util.Iterator;
import de.bea.domingo.*;
import java.util.List;
import java.io.*;
public class LotusNotesDBExtraction
{
public static void main(String[] args) throws DNotesException,IOException
{
    DNotesFactory factory = DNotesFactory.getInstance();
    DSession session = factory.getSession();
    DDatabase database = session.getDatabase("", "employee.nsf");
    List formlist=database.getForms();
    //System.out.println("Number of forms are :"+formlist.size());
    for(int i=0;i<formlist.size();i++)
    {
        DForm formn=(DForm)(formlist.get(i));
        DForm formname= database.getForm(formn.getName());

        **//Creating a class in a file
        FileWriter outputfile = new FileWriter("D:\\Lotusnotesformfiles\\"+formn.getName()+".java",true);
        BufferedWriter out = new BufferedWriter(outputfile);
        out.write("public class "+formn.getName()+" {");
        out.newLine();
        out.flush();**

        try
        {
             **//Compile the class here**   
                            Process p1 = Runtime.getRuntime().exec("cmd javac \"D:\\Lotusnotesformfiles\\"+formn.getName()+".java\"");   
            System.out.println("compiled");
         }   
          catch(IOException x)
        {
            x.printStackTrace();
        }
                **//instantiate the new class here**
}
}

But I am not able to get .class file for the new class. 但是我无法获取新类的.class文件。 Can I instantiate the new class in the same program?? 我可以在同一程序中实例化新类吗? I was stuck at this point. 我被困在这一点上。 Can any please help me 能帮我吗

您应该能够使用Reflection API进行很多(如果不是全部)操作,它允许您在运行时修改类,检查类的内容,甚至动态加载类(例如,在编译后)。

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

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