Mark Jeffords
Mark Jeffords

Reputation: 97

how to run a java .class file in a webpage?

I have saved the following:

import java.util.Map;

public class EnvMap {
public static void main (String[] args) {
    Map<String, String> env = System.getenv();
    for (String envName : env.keySet()) {
        System.out.format("%s=%s%n",
                          envName,
                          env.get(envName));
    }
  }
}

as a .java file and then typed

javac EnvMap.java 

in a Windows Command prompt to compile it into a .class file. It compiled with no errors. I then typed

java EnvMap 

in a Windows Command prompt and the script executed with no errors and showed me all the system information I was looking for.

Now I'm curious how to get this script to run in a webpage. I tried something like:

<html>
<body>
<APPLET CODE=EnvMap.class></APPLET>
</body>
</html>

This did not work.

Any tips are greatly appreciated. Thanks guys!

Upvotes: 0

Views: 5621

Answers (4)

Andrew Thompson
Andrew Thompson

Reputation: 168835

  • As mentioned in other answers:
    1. Server side:, JSP/Servlet.
    2. Client side: applet or app. launched using Java Web Start.
  • If run on the client side, the code will need to be digitally signed by you, and trusted by the user when prompted.

Upvotes: 0

user219882
user219882

Reputation: 15844

First of all, your class have to extend java.applet.Applet class. I suggest you to look at some documentation first...

http://docs.oracle.com/javase/tutorial/deployment/applet/

Upvotes: 1

Yogendra Singh
Yogendra Singh

Reputation: 34367

Your class must extend Applet class as below:

public class EnvMap extends Applet { //<--extends java.applet.Applet
    public void paint(Graphics g) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
           System.out.format("%s=%s%n",
                      envName,
                      env.get(envName));
        }
   }
}

Upvotes: 0

Omaha
Omaha

Reputation: 2292

What web server are you using?

What you really need is a server with a Java/servlet container that will allow you execute Java code as part of the web request processing. For example, Apache Tomcat.

(This is assuming you want to access environment variables on the server. If you want to access them on the client, well, I can't remember too well just how sandboxed an applet's runtime environment is, so you may be out of luck.)

Upvotes: 0

Related Questions