newbie newbie - 4 months ago 18
Java Question

JAVA COMPILER API - Cannot finding result of previous compiled

I have a simple code to make java code compiler

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();
}
}


I don't know why compiler can not find previous result of code before. And I have no idea how to fix that.

For more details, I have making some example :


  1. select A.java
    select A.java

  2. Content of A.java
    result A

  3. then I have select B.java. look A.java has been compiled with create A.class
    enter image description here

  4. B.java can not compile because it can not find
    class A

    enter image description here


Answer

In addition to setting javax.tools.StandardLocation.CLASS_OUTPUT, you need to set javax.tools.StandardLocation.CLASS_PATH to include the same location where you put the previous output:

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));
Comments