Executing code read from a JTextArea

J

Jesper Sahner

Hi!

I use a JTextArea as an area for inputting Java-code, and the text can
be read with the getText()-method.

But how do I execute the code?

Regards,
Jesper
 
R

Rhino

Jesper Sahner said:
Hi!

I use a JTextArea as an area for inputting Java-code, and the text can
be read with the getText()-method.

But how do I execute the code?
You can't just execute it directly; you have to compile it into bytecode
first via 'javac'. If the compile works, you hand the bytecode to 'java' to
execute.

You could do both "under the covers" if you just invoke Ant programatically
and hand it the source code from the JTextArea.

By the way, there is another way of doing what you want to do: if you have
Eclipse, you can use Scrapbook pages to execute code fragments.

Rhino
 
R

Rogan Dawes

Jesper said:
Hi!

I use a JTextArea as an area for inputting Java-code, and the text can
be read with the getText()-method.

But how do I execute the code?

Regards,
Jesper

Take a look at BeanShell

Rogan
 
J

jespersahner

Hi Bill!

Thanx for the link!

The technique used is either calling 'javac' using Runtime.exec() or
com.sun.tools.javac.Main.compile().

However this only solves the problem of compiling and running a "full"
class
as far as I can see.

In general I would like to make all kinds of Java-statements, like:

Integer i = new Integer(5);
int j=7;

and compile and execute it as part of the running session.
Regards,
Jesper
 
J

jespersahner

Hi Rogan!

Looks very interesting. I wonder how BeanShell does the "trick".
Any comments on this?

Regards,
Jesper
 
A

Anton Spaans

Hi Bill!

Thanx for the link!

The technique used is either calling 'javac' using Runtime.exec() or
com.sun.tools.javac.Main.compile().

However this only solves the problem of compiling and running a "full"
class
as far as I can see.

In general I would like to make all kinds of Java-statements, like:

Integer i = new Integer(5);
int j=7;

and compile and execute it as part of the running session.
Regards,
Jesper

Then wrap a dummy class around that code, so that the final compiling code
would be:
public class Dummy1
{
public static void exec()
{
/* Contents of Text-Area here: */
Integer i = new Integer(5);
int j=7;
/* End */
}
}

and call, after compilation the exec() of Dummy1.

Also, take a look at BCEL, in which you can engineer your own bytecode on
the fly:
http://jakarta.apache.org/bcel/

-- Anton.
 
J

jespersahner

Hi Anton!

You could do that, agreed. However then the statements
Integer i=new Integer(5);
int j=7;
only exists within the "scope" of a Dummy1-object as I see it.

Let's say that you want to refer to 'i' and 'j' like e.g.:
Vector v=new Vector();
v.add(i);
int k;
k=j+3;

How would you do that?

Jesper
 
A

Anton Spaans

Hi Anton!

You could do that, agreed. However then the statements
Integer i=new Integer(5);
int j=7;
only exists within the "scope" of a Dummy1-object as I see it.

Let's say that you want to refer to 'i' and 'j' like e.g.:
Vector v=new Vector();
v.add(i);
int k;
k=j+3;

How would you do that?

Jesper

Aha... after the user types in some code (and hits Enter, for example), the
code is executed immediately and all the variables, etc., remain in scope
for the next set of instructions that the user want executed. Am i correct?

If so, then you are writing a scripting language. "Scripted Java" in this
case (not to be confused with the already existing "Javascript"... hehehe).
I don't know if Java (with BCEL or other libraries/tools) is capable of
being scripted that way. Is it possible to have your users use good-old
Javascript instead? If this is possible, there are already ways of doing
that. More info here:
http://jakarta.apache.org/bsf/
-- Anton.
 
A

Anton Spaans

Anton Spaans said:
Aha... after the user types in some code (and hits Enter, for example), the
code is executed immediately and all the variables, etc., remain in scope
for the next set of instructions that the user want executed. Am i correct?

If so, then you are writing a scripting language. "Scripted Java" in this
case (not to be confused with the already existing "Javascript"... hehehe).
I don't know if Java (with BCEL or other libraries/tools) is capable of
being scripted that way. Is it possible to have your users use good-old
Javascript instead? If this is possible, there are already ways of doing
that. More info here:
http://jakarta.apache.org/bsf/
-- Anton.

And using BSF to use Java as a scripting language is called BeanShell: See
also Rogan Dawes post.
-- Anton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,787
Messages
2,569,631
Members
45,338
Latest member
41Pearline46

Latest Threads

Top