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 made an example :
And also can not find class inside package folder.
Here is example 2:
A.java has been compiled with create folder <path>\a\b
and access class using import
I have some try
from :
fileManager.setLocation(javax.tools.StandardLocation.CLASS_OUTPUT, Arrays.asList(dir));
to :
fileManager.setLocation(javax.tools.StandardLocation.CLASS_PATH, Arrays.asList(dir));
but it's not working







examplefolder or do they live in some folder structurea/b?examplefolder, class file inside structurea/bfolder if they are contains package.