Results 1 to 14 of 14
- 06-28-2011, 10:59 PM #1
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?
- 06-28-2011, 11:32 PM #2
See the javax.tools.JavaCompiler package.
- 06-28-2011, 11:50 PM #3
Hmm... Looks interesting. A few shady areas in the docs, but time to experiment! Thanks!
- 06-28-2011, 11:55 PM #4Each object also react to different things, but not all objects should react the same to everything.
- 06-29-2011, 12:07 AM #5
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?
- 06-29-2011, 12:12 AM #6
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
- 06-29-2011, 12:17 AM #7
I use that exact code, and get null. Shucks. Ah well, it's good to know it exists, at least!
- 06-29-2011, 12:19 AM #8
What version and flavor(SE, EE, ??) of the JDK are you using?
- 06-29-2011, 12:23 AM #9
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).
- 06-29-2011, 12:25 AM #10
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.
- 06-29-2011, 01:20 AM #11
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
- 06-29-2011, 03:41 AM #12Moderator
- Join Date
- Feb 2009
- New Zealand
anyone using a program that uses that functionality have to have the SDK installed
- Rep Power
any scripting language I'd have to write would have to be as flexible as possible; having Java-code removes that limitation
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.
- 06-29-2011, 04:25 AM #13
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!
- 06-29-2011, 07:25 AM #14Senior Member
- Join Date
- Jun 2008
- Rep Power
- By quicksun in forum New To JavaReplies: 7Last Post: 02-25-2011, 02:58 PM
- By JavaStudent1990 in forum New To JavaReplies: 4Last Post: 07-29-2010, 09:34 AM
- By Gayethiri_86 in forum New To JavaReplies: 2Last Post: 03-05-2010, 07:43 AM
- By wyldstyle in forum New To JavaReplies: 6Last Post: 02-06-2009, 09:05 PM