Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing Java at runtime

 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I don't know what I'm going to ask is possible or not, but my case is the following:

Is there a way to have Java code in a String and to execute it?

Example:



And then executing this code without converting it to .java then . class!

Is there a way to do that?

Thanks,
 
Henry Wong
author
Marshal
Pie
Posts: 21413
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you are using Java 6, one option is the Java Compiler API. This allows you to call the classes used by the compiler to compile the classes without going through the awkward phase of finding javac and calling runtime.exec(). However, I believe this still generates the class files though.

Another option is to use the Java Scripting API. By default, it only does javascript, but there are a few java scripting engines available. So, you can load a java scipting engine, and use the java scripting API, to run your java, without compiling.

Henry

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic