java-compiler-api

Compile java class in runtime with dependencies to nested jar

守給你的承諾、 提交于 2021-02-08 06:55:45
问题 In a spring-boot app I'm doing the following in runtime: Generating a java class Compiling it Accessing some static fields of the compiled class using reflection. I've based my code on this post and got a problem compiling my generated class in runtime. When running in the IDE compilation works just fine but when runing from a spring-boot jar compilation fails saying symbols are missing or some package does not exist. The class I'm compiling has dependencies to other classes that reside in a

Using JavaCompiler with Classpath referencing jars within ear

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-25 00:12:09
问题 I am working on a project in which an enterprise archive (ear) deployed on a JBoss server needs to compile (and run) a class dynamically. I am using the JavaCompiler class to do this - the complication comes from the fact that the class being compiled has references to some of the classes contained within the ejb jar within the ear. This is not a problem when the deployed ear is 'exploded' on deployment, so it is just a directory rather than an archive - in this case I am able to specify the

Java Compiler API NullPointerException

南楼画角 提交于 2020-01-06 11:55:27
问题 I am using this code to compile a Java file at runtime. First of all, here is my directory tree (in Eclipse). +---- src +----- package +------ Compile.java + + +---- temp +----- anotherpackage +------ Temp.java (file to compile) Here is my code where I am getting the NullPointerException (I already tried using JDK as my Standard VM in Eclipse). public static void compile(URI path, InputStream is, OutputStream os, OutputStream err) throws IOException { SimpleJavaFileObject source = new

Java Compiler API NullPointerException

痴心易碎 提交于 2020-01-06 11:55:06
问题 I am using this code to compile a Java file at runtime. First of all, here is my directory tree (in Eclipse). +---- src +----- package +------ Compile.java + + +---- temp +----- anotherpackage +------ Temp.java (file to compile) Here is my code where I am getting the NullPointerException (I already tried using JDK as my Standard VM in Eclipse). public static void compile(URI path, InputStream is, OutputStream os, OutputStream err) throws IOException { SimpleJavaFileObject source = new

Programmatically compile java with JavaCompiler?

情到浓时终转凉″ 提交于 2020-01-03 15:16:09
问题 I got this Java code from another Stack Overflow thread import java.io.*; import javax.tools.JavaCompiler; import javax.tools.ToolProvider; public class Main { public static void main(String[] args) throws IOException{ String source = " public class Test { public static void main(String args[]) { System.out.println(\"hello\"); } }"; // Save source in .java file. File root = new File("C:\\java\\"); root.mkdir(); File sourceFile = new File(root, "\\Test.java"); Writer writer = new FileWriter

How to set up classpath of JavaCompiler to multiple .jar files using wildcard

只谈情不闲聊 提交于 2020-01-02 08:03:27
问题 I am using JavaCompiler of javax.tools to compile some java code and I am trying to use wildcard in my classpath in order to include all the .jar files but I fail. Here is my code: String classpath = "C:\tomcat6\webapps\myapp/WEB-INF/lib/javax.ws.rs-api-2.0-m10.jar;" + "C:\\tomcat6\\webapps\\myapp/WEB-INF/lib/javax.persistence-2.1.0.jar"; Iterable<String> options = Arrays.asList("-d", classesBaseDir, "-classpath", classpath); JavaCompiler.CompilationTask task = compiler.getTask(null,

Java Dynamic Code Generation with support for generics

妖精的绣舞 提交于 2020-01-01 09:58:20
问题 Is there any tool which provides Java dynamic code generation and that also supports generics? Javassist for example, is the kind of tool that I need, but it does not support generics. I wrote a small lib which uses the Java 6 Compiler API, however as far as I know it depends on JDK. Is there a way to specify another compiler? Or to ship with my application only the parts that I need to invoke with the Java Compiler API? 回答1: It seems you can manipulate and read generic info with Javaassist.

How to compile and run java source code in memory [duplicate]

走远了吗. 提交于 2019-12-29 08:07:47
问题 This question already has answers here : Compile code fully in memory with javax.tools.JavaCompiler [duplicate] (7 answers) Closed 3 years ago . I want to treat a String as a Java file then compile and run it. In other words, use Java as a script language. To get better performance, we should avoid writing .class files to disk. 回答1: This answer is from one of my blogs, Compile and Run Java Source Code in Memory. Here are the three source code files. MemoryJavaCompiler.java package me

How to compile and run java source code in memory [duplicate]

给你一囗甜甜゛ 提交于 2019-12-29 08:07:27
问题 This question already has answers here : Compile code fully in memory with javax.tools.JavaCompiler [duplicate] (7 answers) Closed 3 years ago . I want to treat a String as a Java file then compile and run it. In other words, use Java as a script language. To get better performance, we should avoid writing .class files to disk. 回答1: This answer is from one of my blogs, Compile and Run Java Source Code in Memory. Here are the three source code files. MemoryJavaCompiler.java package me

Can I add a method to a class from a compile time annotation?

房东的猫 提交于 2019-12-18 04:24:37
问题 If I create a custom annotation (example: @SaveFuncName("saveMe") will add a method called saveMe() with some code my processor generates), can the javac compiler use my annotation processor to add a method to the class? Or can I only create a different class? 回答1: Or can I only create a different class? That's correct. The existing API doesn't let us modify existing classes, just generate new ones. Technically speaking, if you want to do some hacky stuff, it's possible to use internal Javac