Java

Moderators: zibadian
Number of threads: 7641
Number of posts: 17980

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Interpreting Java within Java. Posted by ivo jonker on 1 Apr 2008 at 6:44 AM
Hello everyone!

Perhaps a strange question, but my subject says it all.. Im looking for a way to interprete runtime-generated javacode within java.

That is, the generated code should be able to access "hard-coded" functions from the application itself.

Is there a way to do this?
Report
Re: Interpreting Java within Java. Posted by zibadian on 1 Apr 2008 at 11:43 AM
: Hello everyone!
:
: Perhaps a strange question, but my subject says it all.. Im looking
: for a way to interprete runtime-generated javacode within java.
:
: That is, the generated code should be able to access "hard-coded"
: functions from the application itself.
:
: Is there a way to do this?

In Java SE 6 there is: the JavaCompiler interface. JavaDoc: http://java.sun.com/javase/6/docs/api/javax/tools/JavaCompiler.html
If you want to run a script from within your program, you can also use a descendant of the AbstractScriptEngine. JavaDoc: http://java.sun.com/javase/6/docs/api/javax/script/AbstractScriptEngine.html
In Java 5 or earlier, you need to run javac with the correct commandline options from your program.
Report
Re: Interpreting Java within Java. Posted by Josh Code on 7 Apr 2008 at 2:18 AM
Have you looked into Java's reflection package? It is java.lang.reflect

With Java reflection, you can load Class definitions by class name at runtime. You can also access Method arrays. It lets you invoke methods on particular Objects and so on. You can learn more about it from Java's tutorials.

Java reflection works with compiled java code, though.

To keep a degree of simplicity, my advice is to assume the user has an Java SDK version installed and maybe even that the environment variables are properly set so the javac command works. This way, you can use the RunTime class to delegate compiling to the javac command. After compilation, you can then use the reflection package classes to manipulate those new classes and objects at runtime.

It is generally good to reuse things that have been thoroughly developed, and tested because they are more stable. If you want to encourage people to use your software, it is best to work with software that is well established and well known because it tends to be more familiar to those people. Either way prefers the use of Sun's Java compiler and the standard reflection API instead of a java interpreter.


: : Hello everyone!
: :
: : Perhaps a strange question, but my subject says it all.. Im looking
: : for a way to interprete runtime-generated javacode within java.
: :
: : That is, the generated code should be able to access "hard-coded"
: : functions from the application itself.
: :
: : Is there a way to do this?
:
: In Java SE 6 there is: the JavaCompiler interface. JavaDoc:
: http://java.sun.com/javase/6/docs/api/javax/tools/JavaCompiler.html
: If you want to run a script from within your program, you can also
: use a descendant of the AbstractScriptEngine. JavaDoc:
: http://java.sun.com/javase/6/docs/api/javax/script/AbstractScriptEngi
: ne.html
: In Java 5 or earlier, you need to run javac with the correct
: commandline options from your program.

Report
Search for info on rule based systems + Alternatives Posted by ivo jonker on 9 Apr 2008 at 4:31 AM
Sounds like a good one:)

Will definately look into both..

The reason why i was asking for this is that i have been given the assignment to implement a "rule system" into a security system. The rule system should allow the user to dynamically define the functionality (interaction between security sensors and actuators).

Thus far i've found two possibiltys. The first is an interpreted script (either by my own definition->interpreted or translated to java and then interpreted, or perhaps plain java), the second are simple "expert systems" or "rule based systems".

Now, i'm still looking for alternatives or examples/knowledge of simple rule based systems as it is part of the research i have to do for school:)

Any tips on this are welcome!



: Have you looked into Java's reflection package? It is
: java.lang.reflect
:
: With Java reflection, you can load Class definitions by class name
: at runtime. You can also access Method arrays. It lets you invoke
: methods on particular Objects and so on. You can learn more about
: it from Java's tutorials.
:
: Java reflection works with compiled java code, though.
:
: To keep a degree of simplicity, my advice is to assume the user has
: an Java SDK version installed and maybe even that the environment
: variables are properly set so the javac command works. This way,
: you can use the RunTime class to delegate compiling to the javac
: command. After compilation, you can then use the reflection package
: classes to manipulate those new classes and objects at runtime.
:
: It is generally good to reuse things that have been thoroughly
: developed, and tested because they are more stable. If you want to
: encourage people to use your software, it is best to work with
: software that is well established and well known because it tends to
: be more familiar to those people. Either way prefers the use of
: Sun's Java compiler and the standard reflection API instead of a
: java interpreter.
:
:
: : : Hello everyone!
: : :
: : : Perhaps a strange question, but my subject says it all.. Im looking
: : : for a way to interprete runtime-generated javacode within java.
: : :
: : : That is, the generated code should be able to access "hard-coded"
: : : functions from the application itself.
: : :
: : : Is there a way to do this?
: :
: : In Java SE 6 there is: the JavaCompiler interface. JavaDoc:
: : http://java.sun.com/javase/6/docs/api/javax/tools/JavaCompiler.html
: : If you want to run a script from within your program, you can also
: : use a descendant of the AbstractScriptEngine. JavaDoc:
: : http://java.sun.com/javase/6/docs/api/javax/script/AbstractScriptEngi
: : ne.html
: : In Java 5 or earlier, you need to run javac with the correct
: : commandline options from your program.
:
:



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.