如何设置com.sun.tools.javac.Main.compile()函数的类路径? [英] how to set classpath for com.sun.tools.javac.Main.compile() function?

查看:265
本文介绍了如何设置com.sun.tools.javac.Main.compile()函数的类路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 com.sun.tools.javac.Main.compile()函数从我的struts项目运行时编译java文件。但对于一些文件,他们需要一些特定的jar,如axis2。我有jar,但我怎么能设置他们到classpath在运行时编译java文件?我尝试过 System.setProperty(java.class.path,jar dir); 但未能编译。

com.sun.tools.javac.Main 下面的代码为我工作:



Apple.java

  //打包在一个名为MyJavaCode.jar的jar中
import com.xyz.pqr.SomeJavaExamples;
public class Apple {
public static void main(String [] args){
System.out.println(hello from Apple.main());
}
}

AClass.java / p>

  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 {
//使用-cp $旁边指定类路径
//这看起来就像我们为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.java tools.jar 需要在



如果你使用 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>();
//将编译器的类路径设置为与运行时的
相同options.addAll(Arrays.asList( - classpath,System.getProperty(java.class.path)));
//添加更多选项,包括类路径
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final JavaCompiler.CompilationTask task = compiler.getTask(/ * default System.err * / null,
/ * std file manager * / null,
/ * std DiagnosticListener * / null,
/ *编译器选项* / options,
/ *无注释* / null,
Arrays.asList(source));
return task.call();
}

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


I am using com.sun.tools.javac.Main.compile() function to compile java file at run time from my struts projects. But for some files they need some specific jars like axis2. I have the jars but how can i set them to classpath to compile the java file at runtime? I have tried with System.setProperty("java.class.path","jar dir"); but failed to compile.

解决方案

The following code which uses com.sun.tools.javac.Main worked for me:

Apple.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) {}
    } 
}

Note: To compile this AClass.java, tools.jar needs to be in the classpath, which is not there by default, so you will have to specify it.

If you are using Java 1.6 then you should consider using javax.tools.JavaCompiler instead, its getTask() methods takes an argument options which can have the classpath.

For example:

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 is deprecated and undocumented too.

这篇关于如何设置com.sun.tools.javac.Main.compile()函数的类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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