Link to home
Start Free TrialLog in
Avatar of CI-Ia0s
CI-Ia0s

asked on

Running javac() from a java class

I'm nearly done with my project (I have asked two other questions relating to it recently) and am at the point where I need my program to create another program. I found this command to do the task:
com.sun.tools.javac.Main.compile("PROGRAMNAME.java");

However, it returns a "package com.sun.tools.javac does not exist" error. I looked around and found that I needed to update my classpath. So I went to Control Panels -> System -> Environment Variables -> System variables -> Classpath and added this:
";C:\j2sdk1.4.2_05\lib\tools.jar"

I rebooted, but no luck. It still gave me the same error (compiling from NetBeans and from the command line). Has the javac compiler been moved again? Or was it completely removed this time? If it has been moved, is there a way I can access it and get it into my program?
Avatar of Giant2
Giant2

Use Runtime.getRuntime().exec(...)
ASKER CERTIFIED SOLUTION
Avatar of Giant2
Giant2

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
Should be there - but check it

jar -tvf C:\j2sdk1.4.2_05\lib\tools.jar | find "javac"

Avatar of CI-Ia0s

ASKER

It is there... :\ Still get the error. I'll try Runtime.getRuntime()...
Can you post what this reveals from the command line?

echo %CLASSPATH%
Avatar of CI-Ia0s

ASKER

It echos "." which means I should probably have updated the user-defined classpath instead of the system one. However, I've managed to use Runtime.getRuntime() so I'm all set. Thanks anyway!
ok.
Thanks.
>>It echos "."

Yep - that's the problem - the classpath wasn't set properly
Avatar of CI-Ia0s

ASKER

Uh oh. Believe it or not, it wasn't working! (NetBeans is so misleading :( ). I also tried going back to the com.sun.tools method... it now echos correctly, but now a window class that I defined is not found (I assume it's looking for it in the specified classpath :\ ). Now I'm worse off than before. :(

Anyway, I don't care which way I get it to work, but I need it to work one way or the other!
About com.sun.tools.javac.Main I find:
>compile() method takes the familiar command-line input arguments

Put all the classpath & tags usefull to compile your program.
I try to decompile the Main class.
In it's main method it is:
>   public static void main(String args[])
>    {
>        PrintStream printstream = System.err;
>        if(Boolean.getBoolean("javac.pipe.output"))
>            printstream = System.out;
>        Main main1 = new Main(printstream, "javac");
>        System.exit(main1.compile(args) ? 0 : main1.exitStatus);
>    }

so to use this class you must do:
       PrintStream printstream = /*your printStream*/;
       Main main1 = new Main(printstream, "javac");
       boolean ok = main1.compile(/*put here an array containing all the parameter for javac*/);
if (ok){/*compile done correctly*/}
else {/*compile done uncorrectly*/}

Try something like this and tell me if it's ok.
Giant.
>>NetBeans is so misleading

You must find out how it handles its classpath. I'd guess that you have to add libraries which then get appended to the classpath it uses
Avatar of CI-Ia0s

ASKER

Ok. I'm using Runtime.getRuntime() again, and I figured out the problem with it:
I need to make my program wait in between executing commands. The reason is that I have it javac a source file and then jar it. Unfortunately, the program starts a new thread for the javac/jar command and then moves on in the program, usually jarring the class files before they've been compiled. Currently I use Thread.sleep to make it wait for a set amount of time, but, because people's computers run at different speeds, I'd like instead to have it wait until the javac/jar task is done. Anyone have any ideas?
Normally these synchro are solved using wait and notify methods (do not use sleep).
Avatar of CI-Ia0s

ASKER

Wait and notify can not be applied to static methods. :\

Plus, I don't know how to make my Runtime commands notify my main thread when they are done. Unless some one has a really slow computer, this should work... (It waits 2 seconds between each action.) I'd like to be able to get it more exact, though...
wait and notify must be applied to the Thread, not to some static method. (The threads are not static)
Avatar of CI-Ia0s

ASKER

Can I make the runtime notify the thread when the javac command is complete? (could you get me some example code?).
CI-Ia0s, isn't this going on a little too long into new territory after being answered? ;-)
>runtime notify the thread when the javac command is complete
yes. You can use the waitFor method over the Process you use to start the javac.