An Applet That Attempts to Read the user.home System Property - Java Applet

Java examples for Applet:Applet Creation

Description

An Applet That Attempts to Read the user.home System Property

Demo Code

import java.awt.Container;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.swing.JApplet;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class Main extends JApplet {
  JTextArea msgTextArea = null;//www  .j  a v a  2  s . c o m

  @Override
  public void init() {
    String msg = "";
    try {
      String userHome = System.getProperty("user.home");
      msg = "User's Home Directory is '" + userHome + "'";
    } catch (Throwable t) {
      msg = this.getStackTrace(t);
    }
    msgTextArea = new JTextArea(msg, 10, 40);
    Container contentPane = this.getContentPane();
    contentPane.add(new JScrollPane(msgTextArea));
  }

  public String getStackTrace(Throwable t) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw, true);
    t.printStackTrace(pw);
    pw.close();
    return sw.toString();
  }
}

Contents of the readuserhomeapplet.html File Used to View the ReadUserHomeApplet Applet

<html>
        <head>
                <title>Read User Home Directory</title>
        </head>
        <body>
                <applet code="Main"
                        width="400"
                        height="300">
                        This browser does not support Applets.
                </applet>
        </body>
</html>

The following grant block grants this permission to all code:

grant {
        permission java.util.PropertyPermission "user.home", "read";
};

Related Tutorials