Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Is it possible to compile Java-code when running a Java-program?

  1. #1
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Is it possible to compile Java-code when running a Java-program?

    In essence, my situation is thusly: I have a bunch of objects that all have their various values. Each object also react to different things, but not all objects should react the same to everything. This basically leaves me with two possibilities: Either code a scripting language that can be executed at runtime, or hardcode all the various reactions. The first is time-consuming and not as fast as compiled code, and the second is far too static for my liking. Is there a way to write Java-code along with the text-file I read for each object and have that Java-code compiled and put to use?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    See the javax.tools.JavaCompiler package.

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Hmm... Looks interesting. A few shady areas in the docs, but time to experiment! Thanks!

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Each object also react to different things, but not all objects should react the same to everything.
    How do you need to tailor your code at execution time depending on the data that was input?

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    For instance, one part of what I'm trying to do will be a rudimentary AI that can be changed while it's running by simply "re-compiling" the AI. This means that any scripting language I'd have to write would have to be as flexible as possible; having Java-code removes that limitation.

    It seems ToolProvider.getSystemJavaCompiler() returns null though (which means there's no compiler)... Do you know if this is a simple configuration issue in my system, or if that's basically an impassable roadblock?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    I don't know if I did anything special. Here is what I get:

    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    System.out.println("compiler=" + compiler); // compiler=com.sun.tools.javac.api.JavacTool@14ed9ff

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    I use that exact code, and get null. Shucks. Ah well, it's good to know it exists, at least!

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    What version and flavor(SE, EE, ??) of the JDK are you using?

  9. #9
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    The standard J2SE 1.6.0_24. Installed it freshly a few months ago after a complete system re-installation (accompanied by a new harddrive), so it's the only one that exists, without being updated (I think; not sure about that).

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    That's younger than the version I originally tested mine on.

    Are you executing your code with the JRE java or the JDK java.exe?

    Try the JDK java.exe.
    Last edited by Norm; 06-29-2011 at 01:11 AM.

  11. #11
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Ah-HAH! Solved it (kinda)... For some reason, when running "java", it ran the java.exe located in the JRE bin and not the one in the SDK bin. Which, of course, means that anyone using a program that uses that functionality have to have the SDK installed and the path directed to that java.exe instead of the one in the JRE... which isn't a very feasible requirement in my case. Double-chucks.

    EDIT: Tsk for for editing in info that I don't see before I post again :P

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    anyone using a program that uses that functionality have to have the SDK installed
    Yes - because you are asking, basically, that they have a compiler available.

    any scripting language I'd have to write would have to be as flexible as possible; having Java-code removes that limitation
    If you haven't seen it, BeanShell offers a complete Java interpreter.

    If you must load classes at runtime but don't expect the end user to have the jdk, you could create the classes with something like BCEL. I've never done this and it looks rather complicated.
    Jodokus likes this.

  13. #13
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Ah... I must say, Beanshell looks nice at a quick glance, at least. Should suit my needs, if it's as good as it looks. Thanks for that one!

  14. #14
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

Similar Threads

  1. Can't compile and run Java code
    By quicksun in forum New To Java
    Replies: 7
    Last Post: 02-25-2011, 01:58 PM
  2. Java Code Won't Compile
    By JavaStudent1990 in forum New To Java
    Replies: 4
    Last Post: 07-29-2010, 09:34 AM
  3. New to JAVA and code cant compile
    By Gayethiri_86 in forum New To Java
    Replies: 2
    Last Post: 03-05-2010, 06:43 AM
  4. Replies: 6
    Last Post: 02-06-2009, 08:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •