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 thoughts on “Scripting in ASP with Java”

  1. 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.

  2. 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.

  3. 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.

  4. 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

  5. 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

  6. 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

  7. 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

  8. 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.

  9. 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!

  10. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>