java - 如何为 com.sun.tools.javac.Main.compile() 函数设置类路径?

标签 java jar compiler-errors

我正在使用 com.sun.tools.javac.Main.compile() 函数在运行时从我的 struts 项目编译 java 文件。但是对于某些文件,他们需要一些特定的 jar,例如 axis2。我有 jar ,但如何将它们设置为类路径以在运行时编译 java 文件?我尝试使用 System.setProperty("java.class.path","jar dir"); 但编译失败。

最佳答案

以下使用 com.sun.tools.javac.Main 的代码对我有用:

苹果.java

//This class is packaged in a jar named MyJavaCode.jar
import com.xyz.pqr.SomeJavaExamples;
public class Apple {
    public static void main(String[] args) {
        System.out.println("hello from Apple.main()");
    }
}

AClass.java

import com.sun.tools.javac.Main;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class AClass {
    public static void main(String[] args) {
        try {
            //Specify classpath using next to -cp
            //This looks just like how we specify parameters for javac
            String[] optionsAndSources = {
                "-g", "-source", "1.5",
                "-target", "1.5", 
                "-cp", ".:/home/JavaCode/MyJavaCode.jar",
                "Apple.java"
            };
            PrintWriter out = new PrintWriter(new FileWriter("./out.txt"));
            int status =  Main.compile(optionsAndSources, out);
            System.out.println("status: " + status);
            System.out.println("complete: ");
        }catch (Exception e) {}
    } 
}

注意:要编译这个AClass.javatools.jar需要在classpath中,默认是没有的,所以你必须指定它。

如果您使用的是 Java 1.6,那么您应该考虑使用 javax.tools.JavaCompiler,它的 getTask( ) 方法接受一个参数 options,它可以有 classpath

例如:

import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import javax.tools.JavaFileObject;

public final class AClass {
    private static boolean compile(JavaFileObject... source ){
        List<String> options = new ArrayList<String>();
        // set compiler's classpath to be same as the runtime's
        options.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path")));
        //Add more options including classpath
        final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        final JavaCompiler.CompilationTask task = compiler.getTask(/*default System.err*/ null,
            /*std file manager*/ null,
            /*std DiagnosticListener */  null,
            /*compiler options*/ options,
            /*no annotation*/  null,
            Arrays.asList(source));
       return task.call();
}

com.sun.tools.javac.Main 已弃用且未记录。

关于java - 如何为 com.sun.tools.javac.Main.compile() 函数设置类路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246175/

相关文章:

java - 使用 SWT 解决 NoClassDefFoundError 异常

c - 获取 if 语句错误

c++ - 在 C++ : Undefined reference to 上编译时出错

C++ Constexpr 未在此范围内声明

Java多态/抽象类帮助

java - 在 Eclipse 中配置与工作区分开的构建路径库?

java - 能够从 Linux 中的链接运行 jar

jar - 在maven中,如何将非java src文件包含在输出jar中的同一位置?

java - Spring mvc 注释 - javax.el.PropertyNotFoundException : Property 'isOnline' not found on type it. besmart.models.Luce - 但有

java - Spring Integration 事件监听器入站适配器