Aaron Johnson Now with 50% less caffeine!

Posted
15 March 2004 @ 10pm

Tagged
ASP, J2EE, Software Development

Scripting in ASP with Java

I’m working on a project right now that involves a store written in Java using Struts and a sister site written in ASP. One of the features of the store requires that the sister site use some logic written in Java, which you might think is impossible. Turns out (doesn’t it always?) that you can quite easily use simple Java methods and objects within ASP from VBScript. I found two articles (and really only 2) that introduced the use of a simple Java class from ASP (which you can read here and here). Here’s a Hello World example:

package org.mycompany;
public class TestClass {
 public String sayHello(String name) {
   return "Hello " + name;
 }
}

compile this and then you save the resulting class file to:
%Win%/Java/TrustLib/%package%/%classname%.class
So the above example would result in a file saved as:
%Win%/Java/TrustLib/org/mycompany/TestClass.class
From ASP, you can then use the following syntax:

Dim obj
set obj = GetObject("java:org.comcompany.TestClass")
result = obj.sayHello("Aaron Johnson");
Response.Write(result)
set obj = nothing

Couple of items of note:

a) the use of what Microsoft calls a “Java Moniker” allows you to use a Java class without first registering it with the system, which is nice (so you got that going for ya),

b) just like a servlet container, if you make changes to the Java class file while the application is running, you must restart the container, which in this case is IIS,

c) you must (as I mentioned before) make sure to place the compiled class file in the appropriately named subdirectory of %Win%/Java/TrustLib/, where %Win% is usually C:\windows\ or C:\winnt\,

d) you can’t use static methods in your Java class if you want to be able to call those methods from VBScript. It appears (from my quick attempts) that the VBScript engine first creates an object using the default constructor and then calls the given method on that instance. Modifiying the method to be static resulted in a runtime error, and finally

e) your code must work in the Microsoft JVM (I think), which isn’t being supported past September 2004.


15 Comments

Posted by
Ale
16 March 2004 @ 10am

I know there should be a good reason, but why there is a Java and an ASP site in the same project?


Posted by
AJ
16 March 2004 @ 7pm

ASP is the incumbent, it was already being used for the content management of the site, Java was chosen to rebuild the store portion of the site, for a variety of reasons.


Posted by
Joe
8 April 2004 @ 9pm

Aaron,

I have been trying to make your example work and I am having no luck. I keep getting the no moniker error when I execute the asp file. The java class is in the java\trustlib directory.

Any ideas how I can display what Classpath is form my asp application?

Any ideas would be greatly appreciated.


Posted by
AJ
8 April 2004 @ 9pm

hey Joe,

Do you know if the Java class is in a package? If so, you’d have to put the
java class in a sub folder named by the package… ie:
java/trustlib/com/mypackage/MyClass.class

and then call it by package name / class name in VBScript.


Posted by
Joe
8 April 2004 @ 9pm

it’s not!! Tired that…no package, just copied the file into java/trustlib. When I do a GetObject(”java:java.util.date”) that works, so I know the environment is correct. Does it have to be in a jar file??

Is there any way I can find out what GetObject uses for a classpath? I put the specific file location into it but that does not seem to help.

Thanks for your time


Posted by
AJ
8 April 2004 @ 9pm

hey Joe,

> it’s not!! Tired that…no package, just copied the file into
> java/trustlib. When I do a GetObject(”java:java.util.date”) that
> works, so I know the environment is correct. Does it have to be
> in a jar file??
— it definitely doesn’t have to be in a jar file, I am using it right now by just putting the class file into java/trustlib/package/myclass.class.

> Is there any way I can find out what GetObject uses for a
> classpath? I put the specific file location into it but that
> does not seem to help.
— I’m not sure that there is a way to do it programatically… however, you should be able to see where Windows tries to locate the file by using a tool called FileMon (http://www.sysinternals.com/ntw2k/source/filemon.shtml). Filemon shows you all the file accesses that windows does, so you should be able to a)download file mon, b) run it, c) refresh your ASP page, d) watch filemon and see where windows tries to locate the file. You might end up seeing that Windows tries to locate the file but doesn’t have the appropriate permissions to it or something… who knows?

Do you have access to the Java source? Can you share? I could try it on my systems if you want.

Hope that helps.

AJ


Posted by
AJ
13 April 2004 @ 6am

hmm.. that’s really odd. I remember reading that the code runs under the Microsoft JVM, not the Sun JVM, so you might check what version of the Microsoft VM you’re running (not sure how to do that, maybe this: http://www.google.com/search?sourceid=mozclient&ie=utf-8&oe=utf-8&q=microsof
t+jvm+version)

FWIW, I’m running 1.4.1_06.


Posted by
Joe
13 April 2004 @ 6am

and the answer is…compling the java file with target 1.1 did the trick…Thanks for your time and help.


Posted by
David
4 June 2004 @ 8pm

Hi Joe,

What is target 1.1?

I am having the same problems as you were.

Thanks


Posted by
AJ
14 June 2004 @ 8pm

hi,

Target 1.1 is the virtual machine that you want the compiled code to run on:

http://java.sun.com/j2se/1.3/docs/tooldocs/win32/javac.html

-target version
Generate class files that will work on VMs with the specified version. The default is to generate class files to be compatible with both 1.1 VMs and VMs of the Java 2 SDK. The versions supported by javac in the Java 2 SDK are:

1.1
Ensure that generated class files will be compatible with 1.1 and VMs in the Java 2 SDK. This is the default.
1.2
Generate class files that will run on VMs in the Java 2 SDK, v 1.2 and later, but will not run on 1.1 VMs.
1.3
Generate class files that will run on VMs in the Java 2 SDK, v 1.3 and later, but will not run on 1.1 or 1.2 VMs.

AJ


Posted by
Mark van Wyk
19 October 2004 @ 4am

Hi there,

I really don’t want to use the Microsoft Java Virtual Machine - mainly because I can’t find it anywhere. Does anyone have any idea how to get this to work with a Sun JVM?

Thanks,

Mark


Posted by
Chirag
7 May 2005 @ 12pm

Hi there,
I aam working on a project where i need to install my client side application in java that is a class file and then run the classes of that file from my asp page. Now i am stuck with how to directly install my appl on the client machine and then how to call the functions in it.


Posted by
Judy
5 December 2006 @ 11pm

Hello,

I just do some simple testing with create the hello java file, compile, and put to the folder java/trustlib, first time is work, but when i change some coding and recompile, replace the old class file in the folder, the ASP page still display the old code, why? the Class file is no problem, becuase when i try to copy to another PC, the new changing code is display!


Posted by
Dave
16 September 2007 @ 4am

Im a bit late to the party ( almost 2 years, gasp), but…

@ Judy ( well, really @ others with the same issue who found this site, since she is years gone now)

You have to restart the IIS server for you code changes to take effect. Basically your Java class is being read into memory on the server and remaining there. It’s not getting released when you are done with it. To Force a release of the Java classes you must shut down and re-boot IIs. There is another shortcut IIs command to do it without the full reboot, but I can’t remember it anymore. Been a couple of years myself since I used Java with ASP.


Posted by
DAVID
4 July 2008 @ 6pm

This is specifically for MARK and CHIRAG. If you found the answer could you please share.

I am dying to find the answer.


Leave a Comment

Using iText PDF & ColdFusion Java & PGP