Java as a scripting language

X

x x

I am programming an image codec and would like to use a VM to convert
RAW data into RGB data. Is it possible to use Java as a scripting
language for C programs like Lua? As in binding C fuctions to Java
bytecode. Has anyone done this? Or if not is there a Java VM that is
not under GPL that is easy to modify?

I know that JNI exists but it is difficult to use because JNI
implementations were designed to allow Java programs to acces
libraries not to add a script to an existing C program.
 
R

Roedy Green

I know that JNI exists but it is difficult to use because JNI
implementations were designed to allow Java programs to acces
libraries not to add a script to an existing C program.

Scripting with the Java JVM is usually done with JPython, BeanShell or
Groovy.

see http://mindprod.com/jgloss/scripting.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Think of the earth as a living organism that is being attacked by billions of bacteria whose numbers double every forty years. Either the host dies, or the virus dies, or both die."
~ Gore Vidal
 
E

EJP

x said:
I know that JNI exists but it is difficult to use because JNI
implementations were designed to allow Java programs to acces
libraries not to add a script to an existing C program.

That isn't true. It was designed both ways.
 
J

Jon Gómez

x said:
I am programming an image codec and would like to use a VM to convert
RAW data into RGB data. Is it possible to use Java as a scripting
language for C programs like Lua? As in binding C fuctions to Java
bytecode. Has anyone done this? Or if not is there a Java VM that is
not under GPL that is easy to modify?

I know that JNI exists but it is difficult to use because JNI
implementations were designed to allow Java programs to acces
libraries not to add a script to an existing C program.

My two cents about the terminology here.

Do you mean to use Java as a scripting language, or to use some of its
features? There is a distinction between calling methods in Java from
another language and treating Java as a scripting language. The latter
tends to imply that you would use Java as a language to interact with
your program at a higher and simpler level, by providing a more
convenient, simpler interface with which to control your program.
Generally, scripts are loaded and handled at runtime, so they can easily
be changed after you've finished compiling and writing your program, for
example, by the people who use your program. Usually, scripts are
readable in their original source and compiled at runtime.

Java is probably not suitable for this task, being a compiled language,
with fairly rigid syntax, little syntactic sugar, and a rather
significant overhead. Actually, it even has portability concerns
compared to some choices. But if you want, you could manage it.

It would be rather odd though because on the other hand, Lua is designed
to be a scripting language. Using Java as a scripting language on top
of Lua sounds like attaching a car to your bike so you could use the car
to help bike down nature paths.

Jon.
 
J

Jon Gómez

Jon said:
Usually, scripts are
readable in their original source and compiled at runtime.

Correction: Or potentially just interpreted without being compiled.

I was thinking of things along the line of

"Perl has always had a compiler: your source is compiled into an
internal form (a parse tree) which is then optimized before being run."
-- perldoc perlcompile

or

"Ruby codes are compiled into opcodes before executed."
-- Ruby 1.9.1 from 1.8.7 changelog
http://svn.ruby-lang.org/repos/ruby/tags/v1_9_1_0/NEWS


when I made that comment, but I didn't mean to make it sound like this
is always case. In fact, the Ruby example is new--it didn't use to be
that way. Different implementations of whatever languages do their own
things.

Jon.
 
S

Stefan Ram

x x said:
Is it possible to use Java as a scripting language

When writing »throw away« scripts, I am faster sometimes,
because I do not care to do everything perfect. I might
try to use such a style in Java, too, sometimes:

So I define (at least) two levels of a »Java scripting style«.

For example, I wrote this to create an SQL statement:

private static void appendToOutput
( final java.util.ArrayList<java.lang.Throwable> log,
final java.lang.StringBuilder output,
final java.sql.Connection conn )
throws java.sql.SQLException
{ final java.sql.Statement statement = conn.createStatement();
try
{ if( statement.execute( query ))
appendToOutput( log, output, statement ); }
finally
{ try
{ statement.close(); }
catch( final java.lang.Exception exception )
{ log.add( exception ); }}}

The intention was to create industrial-strength code,
but it might be somewhat hard to read and to write.

Sometimes, it might be appropriate to relax requirements.

For example: Sunny-weather coding: Assume there will be
no run-time errors (including exceptions):

private static void appendToOutput
( final java.lang.StringBuilder output,
final java.sql.Connection conn )
throws java.lang.Exception
{ final java.sql.Statement statement = conn.createStatement();
statement.execute( query )
appendToOutput( log, output, statement );
statement.close(); }

I even consider starting with this version in teaching,
because it will help beginners to see the intended flow,
before everything gets hidden unter exception-related code.

Java scripting style level 2 even does not care anymore to
close everything properly, I might even drop »private« and my
beloved »final« and »java.lang«:

static void appendToOutput( StringBuilder output, Connection conn )
throws Throwable
{ conn.createStatement().execute( query )
appendToOutput( log, output, statement ); }

This is dirty code, but as a first step in teaching it might
help to see the intended operation of this method clearly and
might be ok in teaching /if/ the drawbacks and problems of
this style also are explained and a more robust solution is
shown later.

It might also be appropriate, if one needs to get first
/quick/ implementation of something in Java (»quick and dirty«).
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top