java-compiler-api

compiling and running user code with JavaCompiler and ClassLoader

纵饮孤独 提交于 2019-12-17 18:33:46
问题 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

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

元气小坏坏 提交于 2019-12-17 16:46:10
问题 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

How to set classpath when I use javax.tools.JavaCompiler compile the source?

此生再无相见时 提交于 2019-12-17 02:49:09
问题 I use the class javax.tools.JavaCompiler (jdk6) to compile a source file, but the source file depends on some jar file. How to set the classpath of the javax.tools.JavaCompiler ? 回答1: The javax.tools.JavaCompiler#getTask() method takes an options parameter that allows to set compiler options. The following message describes an easy way to set them in order to access the calling program's classpath: You need to configure the standard java file manager to know about the jar files(s) - you use

How can compile I `.java file` in jsp?

我们两清 提交于 2019-12-14 03:31:57
问题 Using java compiler API I want to compile a java file in jsp . I created a html file and using its textArea element I sent its text, which is expected to be code in java entered by user, to JSP on server and after collecting it in a string I made a file with .java extension and wrote it with textArea contents then i used compiler API but it is not compiling the file. I want to do something like this index.html <form action="formAction.jsp" method="post"> Please enter your text: <br/>

Using Java Compiler API to compile multiple java files

烈酒焚心 提交于 2019-12-13 12:06:45
问题 Hi I have requirement to create ,compile and load java classes run time. Using FTL i am creating java source files , and able to compile the source if there is no dynamic dependency. To elaborate with an instance, I have two java source file, one interface and its implementation class. I am able to compile the interface using java compiler api as follows String classpath=System.getProperty("java.class.path"); String testpath =classpath+";"+rootPath+"/lib/is_wls_client.jar;"+rootPath+"/rtds

Dynamically recompile and reload a class

泪湿孤枕 提交于 2019-12-11 10:14:55
问题 I'm building a server in java that can receive java source files, and it should dynamically compile it using JavaCompiler and then load the class. However the problem is that if the server receive a file with the same name but different content, it will still load the previous class and gave the same outputs. I've noticed some answers suggesting creating a superclass for the class I'm trying to load and using a different classLoader, but is it still the case if the java source file is

How to set the source for compilation by a CompilationTask

本小妞迷上赌 提交于 2019-12-11 09:05:41
问题 I do not know how to set the source file for a compilationTask . I tried this: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); List<String> optionList = new ArrayList<String>(Arrays.asList("-d","build/classes")); List<String> classes = new ArrayList<String>(); classes.add("src/Hello.java"); CompilationTask task = compiler.getTask(null, null, null, optionList, classes, null); task.call(); But I get the following error: Exception in thread "main" java.lang.IllegalArgumentException

Can I use JavaCompiler in Google app Engine?

坚强是说给别人听的谎言 提交于 2019-12-11 04:39:04
问题 Is there a way to invoke the Java source compiler via JavaCompiler in Google App Engine? (I didn't see any classes in javax.tools on the white list, so I'm afraid the answer is no ) UPDATE I'm wondering how Java Server Pages work in Google App Engine, since JSPs are compiled to servlets, which obviously would require a Java compiler? 回答1: The answer is no (AFAIK). One reason that you don't see the javax.tools classes in the GAE JRE class whitelist is that they are not JRE classes! You need a

getJavaFileForOutput(…) method of custom JavaFileManager not called by compiler

狂风中的少年 提交于 2019-12-10 10:47:55
问题 I have a custom JavaFileManager that looks something like this: public class InMemoryForwardingFileManager extends ForwardingJavaFileManager<StandardJavaFileManager> { private final Map<String, ByteArrayJavaFileObject> javaFileObjects = new HashMap<>(); @Override public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException{ JavaFileObject fileObject = new ByteArrayJavaFileObject( ... ); javaFileObjects.put(className,

Is there a tool that we could use to compare two Jars at Binary/Byte code level? [closed]

南笙酒味 提交于 2019-12-08 08:14:48
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . The idea is to compare two jars and see if they were generated from same source and compare if they're identical at Binary/Byte code level. Also if they're both compiled with the same compiler i.e. Eclipse JDT or JIT etc compiler. I've looked at Apache Common BCEL, but it only does comparison after decomposing