0

I'm trying to open notepad from the browser using an applet. Yes I know this is awful for security, but it's a proof of concept. I was originally trying to use javascript in APEX to do it, but to no avail. Anyway, Here is my applet:

package opennote;
import java.applet.*;
import java.net.*;


public final class OpenNote extends Applet{

public static void init(String[] args){
    try{
        ProcessBuilder derp = new ProcessBuilder("Notepad.exe","myfile.txt");
        derp.start();
    }
    catch(Exception e){
        System.out.println("Stuff didn't work);
    }
}
}

And my HTML is

<html>
<title>This applet opens the notepad</title>
<hr>
<applet code=OpenNote.class width="320" height="120">
If my browser is Java-enabled, I will open the notepad.
</applet>
<hr>
<html>

It worked when I made the applet as an application, but that isn't saying much. When I open the HTML, it displays the "If my browser..." message before I allow security to run the java applet. Then javascript loads, the message disappears and it gives me an application error. The error is "NoClassDefFoundError", the only information being "OpenNote (wrong name: opennote/OpenNote) The class file is saved in the same directory as the HTML.

Can anyone see what I'm doing wrong? Except the security risk bit of course.

EDIT: I have given up on the notepad proof of concept. It seems to be a hang up. So here is my new problem. I just have an app. It writes an output. It gets an error.

package ex;
import java.applet.*;
import java.net.*;


public final class EX extends Applet{

public static void main(String[] args){
    System.out.println("Here be monsters");
}
}

And my HTML is

<html>
<title>This applet writes stuff</title>
<hr>
<applet code="Ex.class" width="320" height="120">
If my browser is Java-enabled, I will write stuff
</applet>
<hr>
<html>

When I put that in I get the Error

NoClassDefFoundError

Ex(wrong name:ex/Ex)

When I then change the 'code' attribute in the javascript to "ex.Ex.class" I receive the following code:

ClassNotFoundException

ex.Ex.class

What's up with that?

drpogue
  • 133
  • 1
  • 4
  • 12

2 Answers2

1

You need to specify your class correctly in html. Like

<applet code="opennote.OpenNote.class" width="320" height="120">

OR

<applet code="opennote\OpenNote.class" width="320" height="120">

This is because your code is in package opennote.

Mandar Pandit
  • 2,171
  • 5
  • 36
  • 58
  • Returns new error. ClassNotFoundException with the details: "opennote.OpenNote.class" Thank you for your help by the way. – drpogue Jun 27 '14 at 18:43
  • Both forms are incorrect. It should be the Fully Qualified Classname, which is `opennote.OpenNote`. – Andrew Thompson Jun 28 '14 at 10:04
  • These tutorials specifies that, it should be like: ClassName.class.. http://www.tutorialspoint.com/html/html_applet_tag.htm http://www.w3schools.com/tags/tag_applet.asp But it is mentioned that it is not supported in HTML5. – Mandar Pandit Jun 28 '14 at 14:52
  • W3 Schools AKA [W3 Fools](http://www.w3fools.com/) but Oracle's own tutorials often make the same mistake. The `.class` is accounted for, rather than correct - it should be the FQN. BTW - don't forget to add @drpogue (or whoever, to ensure the person is *notified* of a new comment). Oh, and the last HTML version in which the `applet` element was part of the W3C spec. was HTML 3.2. Otherwise we could use `HTML 4.01 transitional`. But that's not the real point. .. – Andrew Thompson Jun 29 '14 at 13:35
  • .. Once the it works for an `applet` element, use the [Deployment Toolkit Script](http://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/depltoolkit_index.html) to write the correct element for whatever version of whichever browser it is launched in. – Andrew Thompson Jun 29 '14 at 13:37
1

The class file is saved in the same directory as the HTML.

That's the wrong place.

  1. If the class is in a Jar it needs to be in the opennote 'sub-directory' of the Jar.
  2. If the class is not in a Jar, it needs to be in the opennote directory which (given no code base) itself needs to be in the same directory as the HTML.

BTW:

  • a safer, cross-platform way to open a File is by using Desktop.getDesktop().open(File)
  • It is unusual (to say the least) that an applet is opening files that reside on the computer of the end user. Why is this applet attempting to do so?
  • Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets.
  • To be valid HTML, that seen above needs a body element. Check the HTML using a validation service.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433