Jump to content


User Tools: Run Applet "directly" In Jcreator


  • Please log in to reply
6 replies to this topic

#1 QFox

QFox

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 05 November 2005 - 02:39 AM

If you want to run an Applet in JCreator, you have to create a webpage, or run it in a project. sometime, this is annoying.

The powerful JCreator tools give you a change to run an Applet "directly" in JCreator.

save the following code to RunApplet.java and compile it.

Create a tool in options

Commands: D:\j2sdk1.4.2_10\bin\java.exe (JAVA.EXE in your JDK folder)
Arguments: RunApplet "$[CurClass]" "$[FileDir]" "$[JavaHome]"
Initial Directory: YOUR FOLDER TO STORE RunApplet.class (this file)

the html text comes from BlueJ.  thanks them a lot.
have fun.  biggrin.gif

CODE
/**
* Utility class to run Applet in JCreator 3.5x
*
* @author CharlesMeng@RRC
* @version November 5, 2005
*
* Configure a JCreator tool in tools menu like this:
*
*  Commands:    D:\j2sdk1.4.2_10\bin\java.exe (JAVA.EXE in your JDK folder)
*  Arguments:    RunApplet "$[CurClass]" "$[FileDir]" "$[JavaHome]"
*  Initial Directory:  YOUR FOLDER TO STORE RunApplet.class (this file)
*
*/


import java.io.*;
import javax.swing.*;

public class RunApplet
{
public static void main(String[] args)
{
 
 // Check command line parameters, if absence, show the Help
 if (args.length == 0)
 {
  String szHelp = "Please configure a JCreator tool like this\r\n\r\n";
  szHelp +=       "    Commands: D:\\j2sdk1.4.2_10\\bin\\java.exe\r\n";
  szHelp +=       "    Arguments: RunApplet \"$[CurClass]\" \"$[FileDir]\" \"$[JavaHome]\"    \r\n";
  szHelp +=       "    Initial Directory: YOUR FOLDER TO STORE RunApplet.class";
  JOptionPane.showMessageDialog(null, szHelp);
  return;
 }
 
 // Get the applet class name
 String szClassName;

 if (args[0].endsWith(".class"))
 {
  szClassName = args[0].substring(0,args[0].length() - 6);
 }
 else
 {
  szClassName = args[0];
 }
 
 // HTML text
  String html ="<html>\r\n";
 html += "\r\n";
 html += "<!-- This file automatically generated by RunApplet.class         -->\r\n";
 html += "<!-- It is regenerated automatically each time the                -->\r\n";
 html += "<!-- applet is run.  Any manual changes made to file will be lost -->\r\n";
 html += "<!-- when the applet is next run through RunApplet.  Save into a  -->\r\n";
 html += "<!-- directory outside of the package directory if you want to    -->\r\n";
 html += "<!-- preserve this file. -->\r\n";
 html += "\r\n";
 html += "<head>\r\n";
 html += "<title>" + szClassName + " Applet</title>\r\n";
 html += "</head>\r\n";
 html += "\r\n";
 html += "<body>\r\n";
 html += "\r\n";
 html += "<h1>" + szClassName +" Applet</h1>\r\n";
 html += "<hr />\r\n";
 html += "<applet code=\"";
 html += szClassName;
 html += ".class\" width=\"500\" height=\"300\" codebase=\".\" archive=\"\" alt=\"Your browser understands the &lt;APPLET&gt; tag but isn't running the applet, for some reason.\">\r\n";
 html += "  Your browser is ignoring the &lt;APPLET&gt; tag!      \r\n";
 html += "</applet> <hr />\r\n";
 html += "\r\n";
 html += "</body>\r\n";
 html += "\r\n";
 html += "</html>\r\n";
 
 //Generate the HTML file
 File fileHtml = new File(args[1], "tmp.htm");

 PrintWriter prtHtml;
 
 try
 {
  prtHtml= new PrintWriter(new PrintStream(new FileOutputStream(fileHtml)));
   prtHtml.write(html);
   prtHtml.close();
 }
 catch (FileNotFoundException ex)
 {
 
 }

 // Call appletviewer to show the applet
 
//  String[] szEnv = {"path=" + args[1] +"\bin"};
 
 String szCmd = "appletviewer.exe tmp.htm";

 fileHtml = new File(fileHtml.getParent());

 try
 {

//   Runtime.getRuntime().exec(szCmd, szEnv, fileHtml);
  Runtime.getRuntime().exec(szCmd, null, fileHtml);
 
 } catch (Exception ex) {
  ex.printStackTrace();
 }

 System.exit(0);
 
}

}


#2 QFox

QFox

    Newbie

  • Members
  • Pip
  • 7 posts

Posted 05 November 2005 - 02:41 AM

btw: Could the developer consider to add a buildin function like this?

it would be great.

#3 javaboy0606

javaboy0606

    Member

  • Members
  • PipPip
  • 16 posts
  • Gender:Male
  • Location:Keller, Texas, USA

Posted 31 January 2007 - 05:41 PM

I agree with the developers having this feature built in.  Up until now every time I wanted to run an applet I had to run appletviewer through MS-DOS prompt, which, as you could imagine, was a totally pain.

--javaboy0606

--javaboy0606

#4 Draco

Draco

    Advanced Member

  • Members
  • PipPipPip
  • 819 posts
  • Gender:Male
  • Location:United Kingdom
  • Interests:Java duh...

Posted 01 February 2007 - 08:47 AM

QUOTE(javaboy0606 @ Jan 31 2007, 05:41 PM) View Post
I agree with the developers having this feature built in.  Up until now every time I wanted to run an applet I had to run appletviewer through MS-DOS prompt, which, as you could imagine, was a totally pain.

--javaboy0606


I don't really see the point to this.  You have to create a .html file in order to use your applet on the web.  So why should JCreator cut out the HTML creation and just do a work around?

A better suggestion would be an "Applet Wizard" that would auto-generate your html file for the applet according to the class that contains main(String[] args).
-----------------------------------
Sun Certified Java Programmer
-----------------------------------
Java 1.5 API <---Check the specs
Google is your friend

#5 Mark01

Mark01

    Newbie

  • Members
  • Pip
  • 9 posts

Posted 01 February 2007 - 12:24 PM

QUOTE(Draco @ Feb 1 2007, 09:47 AM) View Post
I don't really see the point to this.  You have to create a .html file in order to use your applet on the web.  So why should JCreator cut out the HTML creation and just do a work around?

A better suggestion would be an "Applet Wizard" that would auto-generate your html file for the applet according to the class that contains main(String[] args).


It is already there via the Project Wizard. Create an Applet Project, press compile followed by execute, and you are ready to go..

Or use one of the File Wizard templates to create a .html file.



#6 Draco

Draco

    Advanced Member

  • Members
  • PipPipPip
  • 819 posts
  • Gender:Male
  • Location:United Kingdom
  • Interests:Java duh...

Posted 01 February 2007 - 02:06 PM

QUOTE(Mark01 @ Feb 1 2007, 12:24 PM) View Post
It is already there via the Project Wizard. Create an Applet Project, press compile followed by execute, and you are ready to go..

Or use one of the File Wizard templates to create a .html file.


my point exactly wink.gif
-----------------------------------
Sun Certified Java Programmer
-----------------------------------
Java 1.5 API <---Check the specs
Google is your friend

#7 crespowu

crespowu

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 02 July 2007 - 09:27 AM

I have tried the code.It really works. biggrin.gif