Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (577)
games submitted by our members
Games in WIP (498)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Java Compilers  (Read 4107 times)
0 Members and 1 Guest are viewing this topic.
Offline ENC

Junior Member





« Posted 2006-05-30 02: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!
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #1 - Posted 2006-05-30 07: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.


lots of sillystupid games: http://www.emaggame.com
Offline CommanderKeith
« Reply #2 - Posted 2006-05-30 07: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!
Legends of Yore - The Casual Retro Roguelike
Offline ENC

Junior Member





« Reply #3 - Posted 2006-05-30 09:50:45 »

To CommanderKeith

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


To Jansdampf

Thx! Shall try it out...  Grin
Offline Orangy Tang

JGO Kernel


Medals: 51
Projects: 11


Monkey for a head


« Reply #4 - Posted 2006-05-30 10: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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline erikd

JGO Ninja


Medals: 15
Projects: 4
Exp: 14 years


Maximumisness


« Reply #5 - Posted 2006-05-30 13:21:44 »

http://www.janino.net/

Offline endolf

JGO Knight


Medals: 7
Projects: 1


Current project release date: sometime in 3003


« Reply #6 - Posted 2006-05-30 13: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

Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #7 - Posted 2006-05-30 13: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  Wink

lots of sillystupid games: http://www.emaggame.com
Offline purpleguitar

Junior Member





« Reply #8 - Posted 2006-05-30 14: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.
Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2006-05-30 16: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.html

for 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!
Legends of Yore - The Casual Retro Roguelike
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #10 - Posted 2006-05-30 19:20:40 »

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

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

JGO Ninja


Medals: 15
Projects: 4
Exp: 14 years


Maximumisness


« Reply #11 - Posted 2006-05-30 22:18:35 »

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

Offline ENC

Junior Member





« Reply #12 - Posted 2006-05-31 03: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.html

for 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! Smiley
Offline kevglass

JGO Kernel


Medals: 85
Projects: 25


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #13 - Posted 2006-05-31 05:49:38 »

In that case that article is just right for you.

Kev

Offline cylab

JGO Knight


Medals: 34



« Reply #14 - Posted 2006-05-31 09: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!
Offline ENC

Junior Member





« Reply #15 - Posted 2006-06-01 03: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..

Offline cylab

JGO Knight


Medals: 34



« Reply #16 - Posted 2006-06-01 09: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!
Offline ENC

Junior Member





« Reply #17 - Posted 2006-06-05 09: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...

 Grin
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #18 - Posted 2006-06-05 17: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.
Offline CommanderKeith
« Reply #19 - Posted 2006-06-06 04: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.

Offline benjamin

Senior Newbie





« Reply #20 - Posted 2006-06-06 08:10:15 »

Quote
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!
Offline ENC

Junior Member





« Reply #21 - Posted 2006-06-06 13: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...
Offline Ask_Hjorth_Larsen

Junior Member




Java games rock!


« Reply #22 - Posted 2006-06-06 15: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.
Offline CommanderKeith
« Reply #23 - Posted 2006-06-07 08:35:01 »

Thanks for showing me that benjamin, looks like a peice of cake!  Smiley

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?

Offline benjamin

Senior Newbie





« Reply #24 - Posted 2006-06-07 09:14:09 »

I believe that jsr 199 will only be included into jdk and thereby not introduce any new security risks.
Offline CommanderKeith
« Reply #25 - Posted 2006-06-07 09: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=199

That'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?

Offline benjamin

Senior Newbie





« Reply #26 - Posted 2006-06-07 10: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.
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #27 - Posted 2006-06-07 10: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=199

That'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.

弾幕 ☆ @mahonnaiseblog
Offline CommanderKeith
« Reply #28 - Posted 2006-06-07 11:21:01 »

Great, thanks for the link. Pity it doesn't support Java 1.5 stuff yet, but 1.4 is good enough!  Smiley

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.

Offline benjamin

Senior Newbie





« Reply #29 - Posted 2006-06-07 12: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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (23 views)
2014-04-15 18:08:23

BurntPizza (18 views)
2014-04-15 03:46:01

UprightPath (32 views)
2014-04-14 17:39:50

UprightPath (16 views)
2014-04-14 17:35:47

Porlus (32 views)
2014-04-14 15:48:38

tom_mai78101 (58 views)
2014-04-10 04:04:31

BurntPizza (116 views)
2014-04-08 23:06:04

tom_mai78101 (216 views)
2014-04-05 13:34:39

trollwarrior1 (183 views)
2014-04-04 12:06:45

CJLetsGame (190 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!