gpt4 book ai didi

java - Maven编译加载动态类

转载 作者:搜寻专家 更新时间:2023-11-01 03:38:37 26 4
gpt4 key购买 nike

我在使用 mvn exec:java 命令动态编译和加载类时遇到了一些问题。

我创建了一个基于 this tutorial 的小示例它在 IntelliJ 中有效,但在通过命令行执行时失败。

这是我的类:

public class Debug {

public static void main(String[] args) {
DynamicCompiler compiler = new DynamicCompiler();
SayHello hello = compiler.getHello();
hello.sayHello();
}
}

SayHello 界面:

public interface SayHello {
public void sayHello();
}

DynamicCompiler 类:

import javax.tools.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.security.SecureClassLoader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class DynamicCompiler {

private final static String CLASS_PATH = "Hello";

private final static String SOURCE;

static {
StringBuilder builder = new StringBuilder();
builder.append("public class Hello implements SayHello {\n")
.append(" public void sayHello() {\n")
.append(" System.out.println(\"Hello World\")\n;")
.append(" }\n")
.append("}");
SOURCE = builder.toString();
}

public SayHello getHello() {
return compileAndLoadSource(SOURCE, CLASS_PATH);
}

private SayHello compileAndLoadSource(String src, String fullName) {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
JavaFileManager fileManager = new
ClassFileManager(compiler.getStandardFileManager(null, null, null));

List<JavaFileObject> jfiles = new ArrayList<JavaFileObject>();
jfiles.add(new CharSequenceJavaFileObject(fullName, src));

List<String> optionList = new ArrayList<String>();

DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();


JavaCompiler.CompilationTask task =
compiler.getTask(null, fileManager, diagnostics, optionList,
null, jfiles);

boolean success = task.call();
if (!success) {
System.out.println("UNSUCCESSFUL:");
for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
System.out.println(diagnostic.getCode());
System.out.println(diagnostic.getKind());
System.out.println(diagnostic.getPosition());
System.out.println(diagnostic.getStartPosition());
System.out.println(diagnostic.getEndPosition());
System.out.println(diagnostic.getSource());
System.out.println(diagnostic.getMessage(null));
}
return null;
}

try {
Object instance = fileManager.getClassLoader(null).loadClass(fullName).newInstance();
return (SayHello) instance;
} catch (InstantiationException e) {
e.printStackTrace();
return null;
} catch (IllegalAccessException e) {
e.printStackTrace();
return null;
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
}

public static class JavaClassObject extends SimpleJavaFileObject {

/**
* Byte code created by the compiler will be stored in this
* ByteArrayOutputStream so that we can later get the
* byte array out of it
* and put it in the memory as an instance of our class.
*/
protected final ByteArrayOutputStream bos = new ByteArrayOutputStream();

/**
* Registers the compiled class object under URI
* containing the class full name
*
* @param name Full name of the compiled class
* @param kind Kind of the data. It will be CLASS in our case
*/
public JavaClassObject(String name, Kind kind) {
super(URI.create("string:///" + name.replace('.', '/') + kind.extension), kind);
}

/**
* Will be used by our file manager to get the byte code that
* can be put into memory to instantiate our class
*
* @return compiled byte code
*/
public byte[] getBytes() {
return bos.toByteArray();
}

/**
* Will provide the compiler with an output stream that leads
* to our byte array. This way the compiler will write everything
* into the byte array that we will instantiate later
*/
@Override
public OutputStream openOutputStream() throws IOException {
return bos;
}
}

public static class CharSequenceJavaFileObject extends SimpleJavaFileObject {

/**
* CharSequence representing the source code to be compiled
*/
private CharSequence content;

/**
* This constructor will store the source code in the
* internal "content" variable and register it as a
* source code, using a URI containing the class full name
*
* @param className name of the public class in the source code
* @param content source code to compile
*/
public CharSequenceJavaFileObject(String className, CharSequence content) {
super(URI.create("string:///" + className.replace('.', '/') + JavaFileObject.Kind.SOURCE.extension),
JavaFileObject.Kind.SOURCE);
this.content = content;
}

/**
* Answers the CharSequence to be compiled. It will give
* the source code stored in variable "content"
*/
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return content;
}
}

public static class ClassFileManager extends ForwardingJavaFileManager {
/**
* Instance of JavaClassObject that will store the
* compiled bytecode of our class
*/
private JavaClassObject jclassObject;

/**
* Will initialize the manager with the specified
* standard java file manager
*
* @param standardManager
*/
public ClassFileManager(StandardJavaFileManager standardManager) {
super(standardManager);
}

/**
* Will be used by us to get the class loader for our
* compiled class. It creates an anonymous class
* extending the SecureClassLoader which uses the
* byte code created by the compiler and stored in
* the JavaClassObject, and returns the Class for it
*/
@Override
public ClassLoader getClassLoader(Location location) {
return new SecureClassLoader() {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
byte[] b = jclassObject.getBytes();
return super.defineClass(name, jclassObject.getBytes(), 0,
b.length);
}
};
}

/**
* Gives the compiler an instance of the JavaClassObject
* so that the compiler can write the byte code into it.
*/
@Override
public JavaFileObject getJavaFileForOutput(Location location, String className,
JavaFileObject.Kind kind, FileObject sibling) throws IOException {
jclassObject = new JavaClassObject(className, kind);
return jclassObject;
}
}
}

最后是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Debug</groupId>
<artifactId>Debug</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<finalName>pipe</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>Debug</mainClass>
</configuration>
</plugin>
</plugins>
</build>


</project>

当我使用命令 mvn compile exec:java 运行这个示例时,我得到了错误:

 [INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ Debug ---
UNSUCCESSFUL:
compiler.err.cant.resolve
ERROR
30
30
38
string:///Hello.java from CharSequenceJavaFileObject
string:///Hello.java:1: cannot find symbol
symbol: class SayHello

我认为问题在于需要指定类路径和输出目录,但我不确定解决此问题的最佳方法,我已尝试添加以下行:

    optionList.addAll(Arrays.asList("-classpath", "target/classes", "-d", "target/classes"));

这允许它编译,但它仍然找不到类。我也不喜欢使用“目标/类”,因为我觉得它太老套了。

如果能向我展示正确的方法,我将不胜感激!

更新 - 我需要 Maven 依赖项来保持不变。这是一个更新的 main:

import org.jfree.data.xy.XYDataItem;

public class Debug {

public static void main(String[] args) {
DynamicCompiler compiler = new DynamicCompiler();
SayHello hello = compiler.getHello();
hello.sayHello();

//Random new XYDataItem in order to produce the dependency error
XYDataItem xyDataItem = new XYDataItem(10,10);
}
}

并更新了pom.xml http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

    <groupId>Debug</groupId>
<artifactId>Debug</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>jfree</groupId>
<artifactId>jfreechart</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>

<build>
<finalName>pipe</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-cp</argument>
<argument>target/classes/</argument>
<argument>Debug</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>


</project>

我现在看到错误:

    [INFO] --- exec-maven-plugin:1.2.1:exec (default-cli) @ Debug ---
Hello World
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/data/xy/XYDataItem
at Debug.main(Debug.java:9)
Caused by: java.lang.ClassNotFoundException: org.jfree.data.xy.XYDataItem
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

我再次怀疑这与类路径有关,唯一的问题是我无法再次指出它的位置,我尝试使用 maven-shade-plugin 但它似乎没有做任何事。

谢谢!

最佳答案

生成一个新的 Java 进程,即使用 mvn compile exec:exec与以下 pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>Debug</groupId>
<artifactId>Debug</artifactId>
<version>1.0-SNAPSHOT</version>

<build>
<finalName>pipe</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-cp</argument>
<classpath/>
<argument>Debug</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>

<classpath/>将被项目类路径替换。

附言。感谢您分享您的代码,我不知道可以在运行时编译 Java 类!

关于java - Maven编译加载动态类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21350265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com