InMemoryJavaCompiler | Utility class to compile java source code in memory | Plugin library
kandi X-RAY | InMemoryJavaCompiler Summary
kandi X-RAY | InMemoryJavaCompiler Summary
Samples with utility classes to compile java source code in memory. After taking huge effort to look for example on the internet and found nothing work. I decided to create a very simple version. Note: Please make sure you use JDK in your runtime. If you are looking for more examples, please look at unit tests in the src/test/java folder. Artifact is pushed to Sonatype OSS Releases Repository.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Compile single source
- Compile all source code
- Add a source code to the compiler
- Gets the class name
- Returns a JavaFile object for the given class
- Add custom code
- Find class
- Get the byte code as a byte array
- Sets the compiler options
InMemoryJavaCompiler Key Features
InMemoryJavaCompiler Examples and Code Snippets
Community Discussions
Trending Discussions on InMemoryJavaCompiler
QUESTION
With Xtext and Xtend, I have written a DSL grammar as well as its associated code generator which creates a bunch of Java source files making up a Java application. I would like to provide my colleagues with a program which lets them select a file written in our Domain Specific language, runs the code generator to produce the Java source files of the actual Java application, compiles these files and launches the app. This process works fine on my development machine because I have everything installed, including the JDK. But providing my colleagues with this app would force them to install the JDK before using the app. So my question is: is it possible to embed the Java compiler in a distribution package? If not, do you see an other way to proceed? I am using Gradle and javapackager + Inno Setup to generate the distribution package embedding the JRE.
Update #1
...ANSWER
Answered 2018-Jul-02 at 20:43package org.xtext.example.mydsl2.tests
import java.lang.reflect.Method
import java.net.URLClassLoader
import org.eclipse.xtext.util.JavaVersion
import org.eclipse.xtext.xbase.testing.InMemoryJavaCompiler
import org.eclipse.xtext.xbase.testing.JavaSource
class SampleXXX {
def static void main(String[] args) {
val urls = #[]
val classLoader = new URLClassLoader(urls)
val compiler = new InMemoryJavaCompiler(classLoader, JavaVersion.JAVA8)
val result = compiler.compile(new JavaSource("demo/Demo.java", '''
package demo;
public class Demo {
public static void main(String[] args) throws Exception {
Class theClass = Class.forName("demo.Demo", false,
Thread.currentThread().getContextClassLoader());
}
}
'''))
val URLClassLoader runcl = new URLClassLoader(#[], result.classLoader)
(new Thread() {
override run() {
val Class mainClazz = runcl.loadClass("demo.Demo")
val String[] args2 = #["C:\\temp\\memory_a.yyy"]
val Method mainMethod = mainClazz.getMethod("main", typeof(String[]))
mainMethod.invoke(null, #[args2])
}
}=>[
contextClassLoader = runcl
]).start
}
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install InMemoryJavaCompiler
You can use InMemoryJavaCompiler like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the InMemoryJavaCompiler component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page