Olá pessoal,
Estou fazendo o meu TCC e me deparei com uma dificudade em fazer uma rotina que implemente uma compilação em tempo de execução gerando um .class em um diretorio que seleciono. Tentei usar o Runtime.getRuntime().exec() colocando comandos MS-DOS porem não sei oque acontesse que não aceita os comandos.
Estou Usando também a classes ClassLoader para chamada e execução do .class compilado.
A Classe Principal:
package tcc.com.br.testes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.lang.reflect.Method;
/**
*
* @author Filipe
*/
public class MenusClassLoader extends JFrame implements ActionListener {
private JMenu jMenu1;
private JMenu jMenu2;
private JMenuBar jMenuBar1;
private JMenuItem jMenuItens[];
private visualizarArquivos v;
private String diretorio;
private String[] args;
private String[] cmd;
public MenusClassLoader() throws IOException {
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenu2 = new javax.swing.JMenu();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jMenu1.setText("File");
jMenuBar1.add(jMenu1);
jMenu2.setText("Plugins");
v = new visualizarArquivos();
int qtda = v.getContArq();
jMenuItens = new JMenuItem[qtda];
for(int i = 0; i<qtda;i++){
jMenuItens[i] = new JMenuItem();
jMenuItens[i].setText(v.getNomeArq(i));
jMenuItens[i].addActionListener(this);
jMenu2.add(jMenuItens[i]);
}
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
}
public static void main(String args[]) throws IOException {
MenusClassLoader n = new MenusClassLoader();
n.setVisible(true);
n.setSize(400, 400);
}
public void actionPerformed(ActionEvent e) {
String desc = e.getActionCommand();
String pro = "";
diretorio = v.getDiretorio();
ImplementaClassLoader classLoader = new ImplementaClassLoader();
classLoader.setDiretorio(diretorio);
try {
//Aqui eu tento compilar
Process p = Runtime.getRuntime().exec("cmd.exe /c start C:\\Program"
+ " Files\\Java\\jdk1.6.0_21\\bin\\javac ssh1.java");
BufferedReader in = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = null;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
Logger.getLogger(MenusClassLoader.class.getName()).log(Level.SEVERE, null, ex);
}
try {
CustomClassLoader loader = new CustomClassLoader(IntegerPrinterTest.class.getClassLoader());
Class<?> clazz = loader.loadClass(desc);
Object instance = clazz.newInstance();
clazz.getMethod("run").invoke(instance);
} catch (InstantiationException ex) {
Logger.getLogger(MenusClassLoader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(MenusClassLoader.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) {
Logger.getLogger(MenusClassLoader.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) {
Logger.getLogger(MenusClassLoader.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) {
Logger.getLogger(MenusClassLoader.class.getName()).log(Level.SEVERE, null, ex);
} catch (SecurityException ex) {
Logger.getLogger(MenusClassLoader.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(MenusClassLoader.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
A Classe de Tratamento de Arquivos:
package tcc.com.br.testes;
import java.io.File;
import java.io.IOException;
import javax.swing.JOptionPane;
/**
* @author Filipe
*
*/
public class visualizarArquivos {
private String diretorio = "C:/Classes";
private File arquivos;
private String arquivo[];
private String[] arq = new String[3];
private File afile[];
public visualizarArquivos() throws IOException {
arquivos = new File(diretorio);
afile = arquivos.listFiles();
arquivo = new String[getContArq()];
for (int i =0; i<afile.length; i++) {
arquivo[i] = afile[i].getName();
arquivo[i] = getTirarExtencao(getNomeArq(i));
}
}
public int getContArq(){
return afile.length;
}
public String getNomeArq(int posicao){
return arquivo[posicao];
}
public String getTirarExtencao(String ex){
arq = ex.split(".java");
return arq[0];
}
public String getDiretorio(){
return diretorio;
}
}
A Classe que extende ClassLoader:
package tcc.com.br.testes;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author Filipe
*/
public class CustomClassLoader extends ClassLoader {
public CustomClassLoader(ClassLoader parent) {
super(parent);
}
private Class<?> getClass(String name)
throws ClassNotFoundException {
String file = name.replace('.', File.separatorChar)
+ ".class";
byte[] b = null;
try {
b = loadClassData(file);
Class<?> c = defineClass(name, b, 0, b.length);
resolveClass(c);
return c;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
public Class<?> loadClass(String name)
throws ClassNotFoundException {
if (name.startsWith("C:/Classes/")) {
return getClass(name);
}
return super.loadClass(name);
}
private byte[] loadClassData(String name) throws IOException {
InputStream stream = getClass().getClassLoader()
.getResourceAsStream(name);
int size = stream.available();
byte buff[] = new byte[size];
DataInputStream in = new DataInputStream(stream);
in.readFully(buff);
in.close();
return buff;
}
}
Por favor alguem poderia me dar uma dica, seria uma Ajuda Tremenda!