ENC
|
 |
«
Posted
2006-05-30 00:58:58 » |
|
Just would like to know if I can compile a java file from a java file. 1 2 3 4 5 6 7
| 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!
|
|
|
|
Hansdampf
|
 |
«
Reply #1 - Posted
2006-05-30 05:43:31 » |
|
yes, it is possible. do something like 1
| 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.
|
|
|
|
CommanderKeith
|
 |
«
Reply #2 - Posted
2006-05-30 05:46:02 » |
|
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.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
ENC
|
 |
«
Reply #3 - Posted
2006-05-30 07:50:45 » |
|
To CommanderKeith I do not understand you, is it possible for you to explain. To Jansdampf Thx! Shall try it out... 
|
|
|
|
Orangy Tang
|
 |
«
Reply #4 - Posted
2006-05-30 08:23:59 » |
|
yes, it is possible. do something like 1
| 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. 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.
|
|
|
|
erikd
|
 |
«
Reply #5 - Posted
2006-05-30 11:21:44 » |
|
|
|
|
|
endolf
|
 |
«
Reply #6 - Posted
2006-05-30 11:27:22 » |
|
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.
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
|
|
|
|
Hansdampf
|
 |
«
Reply #7 - Posted
2006-05-30 11:44:03 » |
|
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 
|
|
|
|
purpleguitar
|
 |
«
Reply #8 - Posted
2006-05-30 12:51:27 » |
|
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, <i>etc.</i> Works like a charm as long as the client has the java compiler on the path.
|
|
|
|
kevglass
|
 |
«
Reply #9 - Posted
2006-05-30 14:28:22 » |
|
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.htmlfor a useful step by step guide. However, I'm not sure thats what the original poster wanted? Kev
|
|
|
|
Games published by our own members! Check 'em out!
|
|
|
erikd
|
 |
«
Reply #11 - Posted
2006-05-30 20:18:35 » |
|
Janino has that as well but there it's called JavaSourceClassLoader
|
|
|
|
ENC
|
 |
«
Reply #12 - Posted
2006-05-31 01:22:00 » |
|
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.htmlfor a useful step by step guide. However, I'm not sure thats what the original poster wanted? Kev 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! 
|
|
|
|
kevglass
|
 |
«
Reply #13 - Posted
2006-05-31 03:49:38 » |
|
In that case that article is just right for you.
Kev
|
|
|
|
cylab
|
 |
«
Reply #14 - Posted
2006-05-31 07:38:38 » |
|
What do you want to archive? Are you sure, that generating a .java file is the right approach to solve your problem? Just curious...
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
ENC
|
 |
«
Reply #15 - Posted
2006-06-01 01:28:52 » |
|
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..
|
|
|
|
cylab
|
 |
«
Reply #16 - Posted
2006-06-01 07:58:10 » |
|
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).
|
Mathias - I Know What [you] Did Last Summer!
|
|
|
ENC
|
 |
«
Reply #17 - Posted
2006-06-05 07:26:52 » |
|
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... 
|
|
|
|
Ask_Hjorth_Larsen
Junior Devvie  
Java games rock!
|
 |
«
Reply #18 - Posted
2006-06-05 15:51:59 » |
|
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.
|
|
|
|
CommanderKeith
|
 |
«
Reply #19 - Posted
2006-06-06 02:47:07 » |
|
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.
|
|
|
|
benjamin
Senior Newbie 
|
 |
«
Reply #20 - Posted
2006-06-06 06:10:15 » |
|
I haven't investigated it yet, but hopefully its not too hard to use. See and judge for yourself. Test code: 1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Test {
private int hidden = 99;
public getHiddenVariable() { return hidden; }
public static void main(String argv[]) { System.out.println("Hidden variable:" + new Test().getHiddenVariable()); }
} |
Compiler code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| public class CompileTest {
public static void main(String[] args) {
File[] files = { new File("C:/java/Test.java") };
JavaCompilerTool compiler = ToolProvider.getSystemJavaCompilerTool(); StandardJavaFileManager fileManager = compiler .getStandardFileManager(null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager .getJavaFileObjectsFromFiles(Arrays.asList(files)); compiler.getTask(null, fileManager, null, null, null, compilationUnits) .run();
try { fileManager.close(); } catch (IOException e) { e.printStackTrace(); } } |
And standard output: 1 2 3 4
| C:\java\Test.java:5: invalid method declaration; return type required public getHiddenVariable() { ^ 1 error |
Nice error messages as well!
|
|
|
|
ENC
|
 |
«
Reply #21 - Posted
2006-06-06 11:16:45 » |
|
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. I dont really understand is it possible to demo.. or to explain further? So sorry...
|
|
|
|
Ask_Hjorth_Larsen
Junior Devvie  
Java games rock!
|
 |
«
Reply #22 - Posted
2006-06-06 13:50:56 » |
|
Sure. When writing a fractal rendering program some time ago I supported the Mandelbrot set which is a quite beautiful nowhere differentiable set of complex numbers (google it!). It is based on a complex sequence [n+1] = z[n]^2 + c , for all natural numbers n, and the Mandelbrot set is the set of all complex c such that this sequence (with z[0] = 0) converges.
However it is also funny to tinker with this formula, changing the power for example or doing lots of other things. Therefore it would be cool if the user could enter a custom formula at runtime (or similarly just write a java file in a directory without the need to compile the program) to see how this stuff affects the fractal.
The logical 'nice' way to do it is to translate such an expression into some complex algebra using a predefined Complex class with add(), multiply() methods and so on, but doing this results in a performance degradation of 4-5 times! This is why real compilation and use of primitives is useful.
|
|
|
|
CommanderKeith
|
 |
«
Reply #23 - Posted
2006-06-07 06:35:01 » |
|
Thanks for showing me that benjamin, looks like a peice of cake!  Some naive questions: If all of this is provided in the java runtime client VM, do we still need the JDK (except for its other tools)? Will this allow java viruses to take off since they can so easily modify themselves?
|
|
|
|
benjamin
Senior Newbie 
|
 |
«
Reply #24 - Posted
2006-06-07 07:14:09 » |
|
I believe that jsr 199 will only be included into jdk and thereby not introduce any new security risks.
|
|
|
|
CommanderKeith
|
 |
«
Reply #25 - Posted
2006-06-07 07:48:15 » |
|
Rats, I was under the impression that it would be in the standard jre library, I didn't know it would be built on the JDK's javac command. http://jcp.org/en/jsr/detail?id=199That's a bummer since who wants to distribute a JDK to get this feature? Looks like I'll have to find out about Eclipse's JDT and the others mentioned to dynamically compile code. What about the scripting side of things? Do you know if that API will come with the regular JRE?
|
|
|
|
benjamin
Senior Newbie 
|
 |
«
Reply #26 - Posted
2006-06-07 08:49:07 » |
|
Yes jsr 223 should be included in the jre as well. Currently mustang is only shipped with Rhino though other scripting languages support this specifiation as well.
|
|
|
|
oNyx
|
 |
«
Reply #27 - Posted
2006-06-07 08:52:46 » |
|
Rats, I was under the impression that it would be in the standard jre library, I didn't know it would be built on the JDK's javac command. http://jcp.org/en/jsr/detail?id=199That's a bummer since who wants to distribute a JDK to get this feature? Looks like I'll have to find out about Eclipse's JDT and the others mentioned to dynamically compile code. [...] Well, there is... y'know... janino.
|
|
|
|
CommanderKeith
|
 |
«
Reply #28 - Posted
2006-06-07 09:21:01 » |
|
Great, thanks for the link. Pity it doesn't support Java 1.5 stuff yet, but 1.4 is good enough!  I didn't see anything about Rhino, but in the JSR it looks like scripting will not be on the regular JRE either - from http://jcp.org/en/jsr/detail?id=223: 2.2 What is the target Java platform? (i.e., desktop, server, personal, embedded, card, etc.) The target platform is the same as that of Java Servlets, specifically JavaTM 2 Platform, Enterprise Edition (J2EE) 1.4.
|
|
|
|
benjamin
Senior Newbie 
|
 |
«
Reply #29 - Posted
2006-06-07 10:31:57 » |
|
Mozilla Rhino javascript will be available in the latest mustang. Try to google : mozilla rhino mustang for more information.
To make sure that the scripting will ended be shipped in jre I unzipped rt.jar and found the javax.script package... So the scripting will be included in jre and if it says otherwise in the official jsr page then I guess it is not up-to-date.
|
|
|
|
|