Rhino: Script runs from command line, but not from browser

G

gh

I have a short JS program that runs fine from the command line:

#!/usr/bin/java org.mozilla.javascript.tools.shell.Main
print("Hello World");

I saved it as HelloWorld.js. (It's the shabang that makes it a
JavaScript program, not the .js.)

However, if I start it from Safari or Firefox, I get the following
message in the log:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mozilla/
javascript/tools/shell/Main

Any ideas?

(The webserver is Apache on a Mac)
 
T

timsamshuijzen

Any ideas?

Remove the first line from the HelloWorld.js, then it should run in
your browser.
If you want to execute the script from the command line:

/usr/bin/java org.mozilla.javascript.tools.shell.Main HelloWorld.js

Or am I misunderstanding the problem?
 
T

Thomas 'PointedEars' Lahn

I have a short JS program that runs fine from the command line:

#!/usr/bin/java org.mozilla.javascript.tools.shell.Main
print("Hello World");

I saved it as HelloWorld.js. (It's the shabang that makes it a
JavaScript program, not the .js.)

It is the _shebang_ that makes it a Java program that accepts JavaScript
code as input (by running the JavaScript shell). That is different from
being a (server-side) JavaScript script.
However, if I start it from Safari or Firefox, I get the following
message in the log:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mozilla/
javascript/tools/shell/Main

This a Java problem, and so off-topic here. Check your CLASSPATH variable
or use the -classpath argument for the `java' command.

http://www.mozilla.org/rhino/


PointedEars
 
T

timsamshuijzen

How about this one:

#!/usr/bin/java org.mozilla.javascript.tools.shell.Main
alert("Hello World! ... oh, damn, I've popped up on the server side :-
( . Can someone come over to the datacenter and click me away, please?
Help!");
 
T

Thomas 'PointedEars' Lahn

How about this one:

#!/usr/bin/java org.mozilla.javascript.tools.shell.Main
alert("Hello World! ... oh, damn, I've popped up on the server side :-
( . Can someone come over to the datacenter and click me away, please?
Help!");

Please get a minimum clue and refrain from further such nonsense postings.
Thanks in advance.


PointedEars, Score adjusted
 
G

gh

Remove the first line from the HelloWorld.js, then it should run in
your browser.
If you want to execute the script from the command line:

/usr/bin/java org.mozilla.javascript.tools.shell.Main HelloWorld.js

Or am I misunderstanding the problem?

Where I'm going with this is that I want to invoke JS as a result of a
CGI call. I used the browser as an example: it's an easy way to
reproduce the problem. The same error happens when the script is
called via Apache.

It's not unreasonable to post the question here. The people in this
group are most likely to have experience with this situation. Rhino
JavaScript being written in Java does not make it a Java only
question.
 
T

Thomas 'PointedEars' Lahn

It's not unreasonable to post the question here. The people in this
group are most likely to have experience with this situation. Rhino
JavaScript being written in Java does not make it a Java only
question.

Nobody said that it was. However, you get a *Java* error when invoking the
JavaScript shell *Java* class (as you could with any Java class), and so you
should ask in a *Java* group if my classpath suggestion did not help.

JavaScript *programming* issues with Rhino are a completely different
matter, they are on-topic here.

Please provide proper attribution next time. http://jibbering.com/faq/


PointedEars
 
G

gh

I have a short JS program that runs fine from the command line:

#!/usr/bin/java org.mozilla.javascript.tools.shell.Main
print("Hello World");

I saved it as HelloWorld.js. (It's the shabang that makes it a
JavaScript program, not the .js.)

However, if I start it from Safari or Firefox, I get the following
message in the log:
Exception in thread "main" java.lang.NoClassDefFoundError: org/mozilla/
javascript/tools/shell/Main

Any ideas?

(The webserver is Apache on a Mac)

I've solved the problem. It's Apache, not being able to find js.jar.
My guess is that Apache keeps the cgi scripts restricted to the
directories it knows about. If I use the default classpath, as above,
or explicitly specify it (~/Library/Java/Extensions), Apache won't let
it open, resulting in the class not being found.

If I move the js.jar file to my cgi-bin folder, the following script
works just fine:
#!/usr/bin/java -classpath js.jar
org.mozilla.javascript.tools.shell.Main
print("Content-type: text/plain");
print("");
print("Hello World");

(Note that I also had to add a Content-Type and a blank line.)
 
P

Peter Michaux

I've solved the problem. It's Apache, not being able to find js.jar.
My guess is that Apache keeps the cgi scripts restricted to the
directories it knows about. If I use the default classpath, as above,
or explicitly specify it (~/Library/Java/Extensions), Apache won't let
it open, resulting in the class not being found.

If I move the js.jar file to my cgi-bin folder, the following script
works just fine:
#!/usr/bin/java -classpath js.jar
org.mozilla.javascript.tools.shell.Main
print("Content-type: text/plain");
print("");
print("Hello World");

(Note that I also had to add a Content-Type and a blank line.)

I don't think you want to run a Java-based CGI script for production.
Firing up the JVM for each request is much overhead.

Peter
 
G

gh

I don't think you want to run a Java-based CGI script for production.
Firing up the JVM for each request is much overhead.

Peter

I agree - certainly not!

I was looking for a way to do local testing of both the server and
client side on a Mac. Now I can get to work! :)
 
T

Thomas 'PointedEars' Lahn

I was looking for a way to do local testing of both the server and
client side on a Mac. Now I can get to work! :)

You're welcome.


PointedEars
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top