我从另一个Stack Overflow线程获得了这个Java代码
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(sourceFile);
writer.write(source);
writer.close();
// Compile source file.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
compiler.run(null, null, null, sourceFile.getPath());
}
}
Run Code Online (Sandbox Code Playgroud)
但我一直得到像这样的NullPointerException
Exception in thread "main" …Run Code Online (Sandbox Code Playgroud) 我正在研究一个项目,它将涉及我的项目编译足够简单的java文件.根据我的研究,我决定不尝试构建自己的编译器,因为这只需要花费太多时间.
那么有谁知道在这个项目中实现的最佳编译器是什么,以便我能够编译提交给程序的java文件?
我们有一个应用程序,它已从传统的 WAR Spring Web 应用程序迁移到我们希望成为现代 Spring Boot 可执行 Jar 的应用程序。
应用程序模块之一使用JavaCompilerAPI 生成 Java 代码并在运行时对其进行编译。
生成的代码需要驻留在 Web 应用程序类路径中的依赖项,因此我们大致有以下代码:
StandardJavaFileManager standardFileManager = compiler.getStandardFileManager(null, null, null);
List<String> optionList = new ArrayList<String>();
// set compiler's classpath to be same as the runtime's
String classpathString = System.getProperty("java.class.path");
optionList.addAll(Arrays.asList("-nowarn",
"-classpath",
classpathString,
"-g",
"-source",
javaVersion,
"-target",
javaVersion));
Collection classpathFiles = getClasspathJars(classpathString);
addPreviousCompiledClassesToClasspathFiles(rootClassPath, classpathFiles);
try {
File file = new File(getTargetRoot(tempClassPath));
file.mkdirs();
standardFileManager.setLocation(StandardLocation.CLASS_OUTPUT, Lists.newArrayList(file));
standardFileManager.setLocation(StandardLocation.CLASS_PATH, classpathFiles);
// adding current dir to the source path, to find …Run Code Online (Sandbox Code Playgroud) 有没有办法运行JavaCompiler编译的程序?[javax.tools.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.getLineNumber());
tmp += " msg: "+ diagnostic.getMessage(null);
returnErrors.add(tmp.replaceAll("\n", " "));
}
Run Code Online (Sandbox Code Playgroud)
现在我想用寿命1秒运行该程序并获得输出到字符串变量.有什么办法可以吗?
在回答这个问题时,我了解到无法javax.tools从GAE应用程序调用Java Compiler .
这种限制是否仍适用?
如果是这样,我有什么选择将Java源代码"动态"编译成可加载的类文件?
java google-app-engine dynamic-compilation java-compiler-api
我试图创建一个类来编译将在运行时创建的.java,但是我无法使其正常工作。
这是我的代码
File bin = new File(args[args.length-1]);
System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.7.0_60");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager standardJavaFileManager = compiler.getStandardFileManager(null, null, null);
try {
standardJavaFileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(bin));
} catch (IOException e) {
System.err.println("Invalid bin directory");
e.printStackTrace();
}
List<String> options = new ArrayList<String>();
options.add("-Xlint:unchecked");
options.add("-classpath");
options.add("e:\\ParentProject.jar");
System.out.println(options);
CompilationTask compilationTask = compiler.getTask(null, standardJavaFileManager, null, options, null, standardJavaFileManager.getJavaFileObjectsFromFiles(archives));
compilationTask.call();
Run Code Online (Sandbox Code Playgroud)
我正在尝试编译Son.java,以扩展ParentProject.jar中的类,但即使使用“ -classpath”标志,也无法正常工作。但是,如果我将jar添加到编译器项目的构建路径中,它将起作用(并且不需要classpath标志)。这是正常现象吗?
这导致了另一个问题。当我尝试在Tomcat服务器上运行的项目中使用这段代码时,即使使用WEB-INF / lib中的jar,也无法正常工作。
Descriptors.Descriptor我可以使用 DynamicMessage 在运行时动态创建模式 ( ) FileDescriptorProto,也可以使用 DynamicMessage 序列化和反序列化消息。
DynamicMessage然而,由于其构造消息的方式,其性能还不够好。我想知道是否可以在运行时编译模式并在反序列化消息时使用它以获得更好的性能。
如果协议缓冲区不提供在运行时编译模式的方法,那么如果可以将其转换Descriptors.Descriptor为临时 .proto 文件,那么我可以尝试通过从程序调用命令来生成类protoc,并使用 Class 将它们加载到程序中.forName API。
如果我创建一个自定义注释(例如:@SaveFuncName("saveMe")将添加一个saveMe()使用我的处理器生成的某些代码调用的方法),javac编译器是否可以使用我的注释处理器向该类添加方法?或者我可以只创建一个不同的类?
我在网上找到了关于JavaCompiler的代码
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null, null, null,
"src/org/kodejava/example/tools/Hello.java");
Run Code Online (Sandbox Code Playgroud)
它说的compiler.run(null, null, null这三个空是默认System.in,System.out并System.err分别.但是这些值实际上做了什么,因为我找不到这些的API?有人可以解释一下吗?
java compiler-construction api compilation java-compiler-api
我有一个简单的测试程序,只有一个按钮.当用户单击该按钮时,该程序应该创建一个Runnable JAR.Runnable JAR是一个简单的程序,可以在Firefox中打开google.com.该计划有三个班级.
1)Main.java
package test;
public class Main {
public static void main(String[] args) {
Selenium.getGoogle();
}
}
Run Code Online (Sandbox Code Playgroud)
2)Selenium.Java
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Selenium {
public static void getGoogle () {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
}
}
Run Code Online (Sandbox Code Playgroud)
3)TestGUI.java
package test;
import javax.swing.*;
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestGUI extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
public TestGUI() {
setSize(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/* …Run Code Online (Sandbox Code Playgroud) 我想将String视为Java文件,然后编译并运行它.换句话说,使用Java作为脚本语言.
为了获得更好的性能,我们应该避免将.class文件写入磁盘.
我有类文件有这样的方法:
public boolean validate(String str) {}
Run Code Online (Sandbox Code Playgroud)
这个验证方法中有很多代码,但我只想让它总是返回true或false.有人能指出我如何修改类文件来实现这一目标吗?
bytecode classloader bytecode-manipulation java-compiler-api
使用java compiler API我想编一个java file在jsp.我创建了一个html文件并使用它的textArea元素我将其文本发送到服务器上的JSP,并将其收集到一个字符串中.java,textArea然后将其写入带有扩展名的文件,然后将其写入使用了编译器API,但它没有编译文件.我想要做的事像这样
index.html
<form action="formAction.jsp" method="post">
Please enter your text:
<br/>
<textarea name="textarea1" rows="5"></textarea>
<br/>
<input type="SUBMIT" value="Submit"/>
</form>
Run Code Online (Sandbox Code Playgroud)
formAction.jsp
<%
String convert = request.getParameter("textarea1");
PrintWriter wr = response.getWriter();
File f =new File("file121.java");
if(!f.exists())
{
f.createNewFile();
wr.println("File is created\n");
}
FileWriter write = new FileWriter(f);
BufferedWriter bf = new BufferedWriter(write);
bf.write(convert);
bf.close();
wr.println("File writing done\n");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null,null,null,"file121.java");
if(result==0){
wr.println("Compilation …Run Code Online (Sandbox Code Playgroud) java ×9
annotations ×1
api ×1
bytecode ×1
classloader ×1
compilation ×1
jsp ×1
jsr199 ×1
spring-boot ×1