我有一个简单的代码来制作Java代码编译器
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.tools.*;
import java.io.*;
import java.util.*;
public class Compiler extends JFrame
{
String loc="D:\\java";
File file=null,
dir=null;
boolean success;
public Compiler()
{
super("Highlight example");
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
}
catch (Exception evt) {}
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(1,2));
JTextPane textPane = new JTextPane();
JTextArea debug=new JTextArea();
JButton comp=new JButton("Compile"),
browse=new JButton("...");
JTextArea location=new JTextArea();
JPanel right=new JPanel(),
up=new JPanel();
up.setLayout(new BorderLayout());
up.add(new JScrollPane(location),"Center");
up.add(browse,"East");
right.setLayout(new BorderLayout(2,1));
right.add(up,BorderLayout.NORTH);
right.add(new JScrollPane(textPane), "Center");
right.add(comp,"South");
add(right);
add(new JScrollPane(debug));
setSize(800, 400);
setVisible(true);
browse.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
success=false;
UIManager.put("FileChooser.readOnly", Boolean.TRUE);
JFileChooser fc = new JFileChooser(loc);
int status = fc.showOpenDialog(new JFrame());
if (status==JFileChooser.APPROVE_OPTION)
{
debug.setText("");
file = fc.getSelectedFile();
dir = fc.getCurrentDirectory();
try
{
textPane.read(new FileReader(file), null);
}
catch(Exception ex)
{
}
}
}
});
comp.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList(file+""));
String[] option=new String[]{"-g","-parameters"};
Iterable<String> options = Arrays.asList(option);
JavaCompiler.CompilationTask task=null;
fileManager.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(dir));
task = compiler.getTask(null, fileManager, diagnostics, options, null, compilationUnits);
success = task.call();
fileManager.close();
// System.out.println("Success: " + success);
if(success==true)
{
debug.setText("Success");
}
else
{
int i=1;
for (Diagnostic diagnostic : diagnostics.getDiagnostics())
if(diagnostic.getLineNumber()!=-1)
debug.append("Error on line "+diagnostic.getLineNumber()+" in "+ diagnostic+"\n");
}
}
catch(Exception ex)
{
}
}
});
}
public static void main(String[]args)
{
new Compiler();
}
}
我不知道为什么编译器之前找不到以前的代码结果。我不知道如何解决这个问题。
有关详细信息,我举了一个例子:
并且在包文件夹中也找不到类。
以下是示例2:
A.java
已使用创建文件夹<path>\a\b
并使用导入访问类
我试了一下
来自:
fileManager.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(dir));
到:
fileManager.setLocation(javax.tools.StandardLocation.CLASS_PATH, Arrays.asList(dir));
但它无效
答案 0 :(得分:1)
除了设置javax.tools.StandardLocation.CLASS_OUTPUT
之外,您还需要将javax.tools.StandardLocation.CLASS_PATH
设置为包含放置上一个输出的位置:
fileManager.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(dir));
// Set dirClassPath to include dir, and optionally other locations
fileManager.setLocation(javax.tools.StandardLocation.CLASS_PATH, Arrays.asList(dirClassPath));
答案 1 :(得分:1)
您需要指定sourcepath目录, 你的代码
String[] option=new String[]{"-g","-parameters"};
,
试试这个
String[] option=new String[]{"-g","-sourcepath", loc};
其中loc
是B
类的根目录。 A
正确存储在自己的包中。
我已经通过修改尝试了您的代码 http://kurungkurawal.com/gifs/tut-compile.gif
答案 2 :(得分:0)
创建外部类的jar文件并附加编译器。
List<String> optionList = new ArrayList<String>();
// set compiler's classpath to be same as the runtime's
optionList.addAll(Arrays.asList("-classpath", System.getProperty("java.class.path")
+ getValueFromPropertieFile("jarfile1;jarfile2")));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, optionList, null,
compilationUnit);