Java Compilers

Just would like to know if I can compile a java file from a java file.


import java.lang.*;
public class Driver{
	public static void main (String []args){
		boolean results = Compiler.compileClasses("Test.java");
		System.out.println(results);
	}
}

Like the code above is it possible? Cause some of my friends says that is not possible to do so.

Or is it a more complex process to get a java file to compile another java file?

Thanks!

yes, it is possible.
do something like

new com.sun.tools.javac.Main().compile(new String[] {pathtofile});

which returns a status code, then you can load the generated .class and run it.

If you are asking whether you can compile a file from text (java code), it is not possible using Java 5.

But apparently you can do it using a package from Eclipse called the JDT. In Mustang (Java 6) you will be able to do it using the new compiler API.

To CommanderKeith

I do not understand you, is it possible for you to explain.

To Jansdampf

Thx! Shall try it out… ;D

IIRC you’re not supposed to use (or even be aware of) the classes in the com.sun.* package, and they may or may not be present in any given JRE (or they may be present but work in different ways). If you use them you’ll have to be very careful to catch all possible errors and have some kind of fallback.

http://www.janino.net/

Thats as I recall too. com.sun.* classes/packages are sun internal things. If you are using a Sun JDK, then you may well be able to use them to compile classes (and I have done), but, and it’s a big but, you won’t be able to use any other providers JDK for it, or the sun JRE. If you are thinking of doing the compile in a server side process, that runs on hardware you control, then it’s not too much of an issue.

Don’t count on those classes being there in Java 6.

Endolf

I use that approach instead of other scripting languages and for pure development only. I have a neat little text window, press F9 to compile and load, and after 0.2 sec there spawns a new foe with freshly compiled behaviour. I think there is no advantage over other scripting languages (Beanshell, which I used before) but I like to code in a not castrated style :wink:

Easy way out:


/**
 * Compile the program at the given path.
 * This method waits until the compilation is finished.
 * @param file the file to compile
 */
public void compileAndWait(File file) {
  try {
    Process p = Runtime.getRuntime().exec("javac " + file);
    p.waitFor();
   } catch (Exception e) {
    e.printStackTrace();
   }
}

Depending on your needs, you could set parameters, catch return values, dump output to a stream, process specific exceptions, etc. Works like a charm as long as the client has the java compiler on the path.

Actually the com.sun.* classes you need to compile are speced and are intended to exist on all Sun Java platforms. However, you can’t use a JRE - you need an SDK to compile (pre 1.5 assuming that scripting JSR got included). To get access to the compiler classes you need to include “tools.jar” on your classpath - which comes with the SDK.

Check out:

http://www.javaworld.com/javatips/jw-javatip131.html

for a useful step by step guide.

However, I’m not sure thats what the original poster wanted?

Kev

http://www.panix.com/~mito/articles/articles/classloader/j-classloader-ltr.pdf

This document describes a CompilationClassLoader, which will dynamically compile source code.

Janino has that as well but there it’s called JavaSourceClassLoader

Hi Kev,

For me is that I am writing a program that will

  1. Write out a .Java file via File IO

  2. Compile the outputed .Java file

  3. Use the .class file from the compiled .Java file

So that is why I ask is it possible to compile a .java file within a .java file

Thanks! :slight_smile:

In that case that article is just right for you.

Kev

What do you want to archive? Are you sure, that generating a .java file is the right approach to solve your problem?
Just curious…

Hi cylab, what do you mean by that… I dont really understanding…

I am just curious to know, why you have to generate a java file and compile it at runtime. Since I don’t know what you program does (or should do), I wonder if there are better ways to do what you are trying to do (without the java generating and compiling process).

Basically I am trying to protect the data from being seen from the users.

Cause when it is in the .class file the information is hidden from the user as everything is seen in byte code.

Another reason is that i am trying to learn the concept of encryption by writing this program.

Alot of my friends find it stupid, cause java class files is easily decompiled by many decompilers since is avaiable almost anywhere.

But I just want to get the concept of it…

;D

Security by obscurity.

This has nothing to do with encryption.

Actually it is probably counterinstructive.

A good situation in which you may want to compile during runtime is when you have some semi-scripting-like needs. For example if you want to do calculations using a mathematical formula it will be faster to compile it than to rely on some home-made library which uses classes for data representation and method calls for operations and so on.

This is why I can’t wait to use Mustang’s new Compiler API. I haven’t investigated it yet, but hopefully its not too hard to use.