Hello everyone,
I've been looking for a solution to my problem for the past two days and still could not solve it. First, let me explain what exatly I want to do...
I'm developing a code generator which generates plain simple java code. I want to compile this code inside my program and use the classes. At the beginning, I compiled and ran the code by spawning a child process that runs "javac myclass.java" and "java myclass" like commands. But later I noticed that, this is not the right way of doing this and tried to understand the reflection API and accessing the javac from inside the program.
Current status: I can create instances of my generated classes using the reflection API. What I can't do is: I can't compile any java file using the source code below:
String fn = "MyFileName.java";
com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
int compileReturnCode = javac.compile(new String[]{fn});
When I run it as a class with command line:
C:\Borland\JBuilder2005\jdk1.4\bin\javaw -classpath "D:\skoolstuff\cse497-8-project\sources\GCGp2\classes;
D:\skoolstuff\cse461-compiler\Assignment3\includes\mj1.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\javaws\javaws.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\charsets.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\dnsns.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\ldapsec.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\localedata.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\ext\sunjce_provider.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\im\indicim.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\im\thaiim.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\jce.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\jsse.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\plugin.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\rt.jar;
C:\Borland\JBuilder2005\jdk1.4\jre\lib\sunrsasign.jar;
C:\Borland\JBuilder2005\jdk1.4\lib\dt.jar;
C:\Borland\JBuilder2005\jdk1.4\lib\htmlconverter.jar;
C:\Borland\JBuilder2005\jdk1.4\lib\tools.jar" bb.gcg.GCG
it works flawless. But when I pack it in a JAR file with a proper manifest pointing bb.gcg.GCG class as main class, it spawns a runtime exception just like this:
java.lang.NoClassDefFoundError: com/sun/tools/javac/Main
at bb.gcg.ToJava.compile(ToJava.java:48)
ToJava.java's 48th line is the line that contains
Main javac = new Main();
sentence. As far as I know, to use
com.sun.tools.javac.Main
class, a command line like this should be used while running the JAR:
java -classpath c:\path\to\jdk\lib\tools.jar;. -jar MyJar.jar
It doesn't work either. I work on windows and I'm open to all suggestions. Thanks in advance,
Burak
Note: I tried uninstalling and reinstalling Java, current version is:
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
Before it was some 1.5.0xx