Javascript using Java methods

Discussion in 'Javascript' started by annie, Dec 21, 2004.

  1. annie

    annie Guest

    Hi all.

    Can Java classes/objects be used from within the javascript code on the
    HTML page?
    I.e., can I call a Java method from the javascript function?
    Thanks in advance

    Anna
     
    annie, Dec 21, 2004
    #1
    1. Advertising

  2. annie wrote:


    > Can Java classes/objects be used from within the javascript code on the
    > HTML page?
    > I.e., can I call a Java method from the javascript function?


    If you embed a Java applet e.g.
    <applet name="appletName" code="applet.class"></applet>
    then in some browsers you can call the public methods of that applet e.g.
    document.applets.appletName.methodName();
    It however depends on the browser and the used Java virtual machine
    whether that works, if you use the Sun JRE for Java and browsers like
    Mozilla 1.4 or later, Opera 7, IE 5/6 on Windows then it works, IE on
    Win with the MS Java VM should also work. I think it doesn't work with
    MSIE/Mac while the latest Safari versions are reported to support
    JavaScript-->Java applet communication.

    In Mozilla and in Opera with the Sun JRE you can also directly script
    Java, I have only tested that on Windows, but there you can do e.g.
    var javaDate = new java.util.Date();
    but that doesn't work with MSIE, whatever Java (Sun or MS) you use.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 21, 2004
    #2
    1. Advertising

  3. Hi,

    annie wrote:

    > Hi all.
    >
    > Can Java classes/objects be used from within the javascript code on the
    > HTML page?
    > I.e., can I call a Java method from the javascript function?
    > Thanks in advance
    >
    > Anna


    In Mozilla, you can.

    Example:

    netscape.security.PrivilegeManager.enablePrivilege(
    "UniversalFileRead" );

    var flCheck = new java.io.File( strFullPath );
    bFileExists = flCheck.exists();

    Or:

    netscape.security.PrivilegeManager.enablePrivilege(
    "UniversalFileWrite" );
    var fsOutput = new java.io.FileOutputStream( strFullPath );
    var flOutput = new java.io.DataOutputStream( fsOutput );

    flOutput.writeBytes( strContent );

    flOutput.flush();
    fsOutput.close();


    On other browsers, one solution is to pack your Java classes in an
    applet and to use LiveConnect (if supported) to access the applet's
    public classes.

    See
    http://www.galasoft-lb.ch/myjavascript/consulting/LiveConnect102

    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, Dec 21, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neo
    Replies:
    1
    Views:
    531
    Scott Allen
    Jan 7, 2005
  2. Buster Copley
    Replies:
    5
    Views:
    568
    Gianni Mariani
    Jul 7, 2003
  3. Nikita Petrov
    Replies:
    2
    Views:
    121
    Gary Wright
    Apr 6, 2008
  4. Kenneth McDonald
    Replies:
    5
    Views:
    342
    Kenneth McDonald
    Sep 26, 2008
  5. Replies:
    0
    Views:
    439
Loading...

Share This Page