gpt4 book ai didi

JAVA 编译器 API - 无法找到外部类文件

转载 作者:行者123 更新时间:2023-11-30 02:51:48 26 4
gpt4 key购买 nike

我有一个简单的代码来制作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();
}
}

我不知道为什么编译器找不到以前的代码结果。我不知道如何解决这个问题。

有关更多详细信息,我做了一个示例:

  1. 选择A.java select A.java

  2. A.java的内容 result A

  3. 然后我选择了 B.java。看看A.java已经用create A.class编译好了 enter image description here

  4. B.java 无法编译,因为找不到 class A enter image description here

并且在包文件夹中也找不到类。

这里是示例 2:

enter image description here

A.java已使用创建文件夹 <path>\a\b 进行编译

enter image description here

并使用导入访问类

enter image description here

我有一些尝试

来自:

fileManager.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(dir));

至:

fileManager.setLocation(javax.tools.StandardLocation.CLASS_PATH, Arrays.asList(dir));

但它不起作用

最佳答案

除了设置 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));

关于JAVA 编译器 API - 无法找到外部类文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38420057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com