java-compiler-api

Does javax.tools depend on the JDK?

早过忘川 提交于 2019-11-30 14:22:02
I want to use JavaCompiler to dynamically create some classes. I found the source code of the javax.tools package, but there is no implementation; some posts on the internet say it depends on tools.jar , I am not sure tools.jar associates with JRE. So, can I run the program in a JRE environment without JDK installed? Another question, what is the implementation detail of JavaCompiler , is it creating a new process to invoke the javac command? thanks Stephen Denne JRE's need to include the interfaces, and classes in that package, but do not need to provide implementations. Sun's JRE doesn't,

Dynamic Compiling Without Create Physical File

感情迁移 提交于 2019-11-29 17:27:48
I follow the tutorial from Generating Java classes dynamically through Java compiler API , the code is work but what I see is the program will create a class file after compiling it. import java.io.IOException; import java.net.URI; import java.util.Arrays; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.tools.JavaCompiler.CompilationTask; import javax.tools.*; public class Compiler { static final Logger logger = Logger.getLogger(Compiler.class.getName()); static String sourceCode = "class HelloWorld{" + "public static void main (String

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

拥有回忆 提交于 2019-11-29 12:13:49
This question already has an answer here: Compile code fully in memory with javax.tools.JavaCompiler [duplicate] 7 answers 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. soulmachine 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.soulmachine.compiler; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; import java.lang.reflect

android studio with Java compiler error: string too large to encode using UTF-8 written instead as 'STRING_TOO_LARGE'

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 09:10:19
When I clean the android project in android studio, the error happen, I have backed to previous commit or different branch, which works find couple days ago, but has this error now. I have checked this question and there is not large image or strings added for my project. STRING_TOO_LARGE String in Kothlin For the time being, you can downgrade Gradle version to resolve this issue. Use gradle 3.1 version like 3.1.3 below. classpath 'com.android.tools.build:gradle:3.1.3' ILMTL I was stuck on this problem and read through this topic and no one provided a future solution. I did NOT want to revert

Dynamic Compiling Without Create Physical File

旧时模样 提交于 2019-11-28 12:34:55
问题 I follow the tutorial from Generating Java classes dynamically through Java compiler API, the code is work but what I see is the program will create a class file after compiling it. import java.io.IOException; import java.net.URI; import java.util.Arrays; import java.util.Locale; import java.util.logging.Level; import java.util.logging.Logger; import javax.tools.JavaCompiler.CompilationTask; import javax.tools.*; public class Compiler { static final Logger logger = Logger.getLogger(Compiler

Why does Java not show an error for double semicolon at the end of a statement?

守給你的承諾、 提交于 2019-11-28 12:05:08
I accidentally wrote a java statement with two semicolons at the end. The java compiler does not show any error and it runs. Code: System.out.println("Length after delete the text is "+name.length());; For learning purposes I tried adding different characters after the semicolon, and the java compiler has shown the compile time error as Syntax error on token ")", delete this token . This statement: System.out.println("Length after delete the text is "+name.length());) Why does java treat the semicolon and other characters as different? Because a double semicolon is not treated as a double

compiling and running user code with JavaCompiler and ClassLoader

断了今生、忘了曾经 提交于 2019-11-28 07:51:21
I am writing web app for java learning. Using which users may compile their code on my serwer + run that code. Compiling is easy with JavaCompiler: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>(); CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, prepareFile(nazwa, content)); task.call(); List<String> returnErrors = new ArrayList<String>(); String tmp = new String(); for (Diagnostic diagnostic : diagnostics.getDiagnostics()) { tmp = String.valueOf(diagnostic

How do I use JDK6 ToolProvider and JavaCompiler with the context classloader?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 21:39:06
My usage case is compiling generated source files from a java program using the ToolProvider and JavaCompiler classes provided in JDK 6. The source files contain references to classes in the context classloader (it runs in a J2EE container), but not in the system classloader. My understanding is that by default the ToolProvider will create the JavaCompiler instance with the system classloader. Is there a way to specify a classloader for JavaCompiler to use? I tried this approach, modified from something on IBM DeveloperWorks: FileManagerImpl fm = new FileManagerImpl(compiler

Is it possible to programmatically compile java source code in memory only?

旧街凉风 提交于 2019-11-27 06:54:25
I have found many references explaining how to programmatically compile a Java class using the JavaCompiler class: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); int result = compiler.run(null, null, null, "a_file_name"); However, I would like to know if there is an open source library that let me compile source code generated programmatically (therefore without a src file being involved) and generate some byte code in an output stream (without generating a class file in the file system). For example, I am looking for being able to write something like this: InputStream input =

How do I use JDK6 ToolProvider and JavaCompiler with the context classloader?

梦想的初衷 提交于 2019-11-26 20:45:21
问题 My usage case is compiling generated source files from a java program using the ToolProvider and JavaCompiler classes provided in JDK 6. The source files contain references to classes in the context classloader (it runs in a J2EE container), but not in the system classloader. My understanding is that by default the ToolProvider will create the JavaCompiler instance with the system classloader. Is there a way to specify a classloader for JavaCompiler to use? I tried this approach, modified