Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking java application into java applet then loading in html
    text
    copied!<p>I had/have a java application which is loading a game, and i tried to make it into an applet and loading into a html webpage, but when i try and load it i get and error and it doesn't load, could someone please help me and tell me what i am doing wrong?</p> <p>Java application code:</p> <pre><code>import java.applet.*; import java.awt.Dimension; import java.io.*; import java.net.*; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.*; public class OSRSLoader implements AppletStub { static JMenuBar menubar; static JFrame i; public static void main(String args[]) throws Exception { new OSRSLoader(); } public OSRSLoader() throws Exception, FileNotFoundException { JFrame i = new JFrame("RS Oldschool Client Beta V1:00"); i.setSize(new Dimension(763, 514)); i.setPreferredSize(new Dimension(763, 514)); JLabel label = new JLabel(); ImageIcon icon = new ImageIcon(new URL("http://www.runescape.com/image/rsp777/oldschool_ani.gif")); icon.setImageObserver(null); label.setIcon(icon); i.add(label); i.pack(); i.setVisible(true); String str = getPageSource(new URL("http://oldschool38.runescape.com")); Pattern pattern = Pattern.compile("gamep\\w+"); Matcher match = pattern.matcher(str); if(match.find()) { OSRSLoader stub = new OSRSLoader(Pattern.compile("&lt;param name=\"([^\\s]+)\"\\s+value=\"([^&gt;]*)\"&gt;"), str); Download("http://oldschool38.runescape.com/", (new StringBuilder(String.valueOf(match.group(0)))).append(".jar").toString()); stub.setCodeBase(new URL((new StringBuilder("http://oldschool38.runescape.com/")).append(match.group(0)).append(".jar").toString())); stub.setDocumentBase(new URL((new StringBuilder("http://oldschool38.runescape.com/")).append(match.group(0)).append(".jar").toString())); stub.getParameter((String)parameters.get("java_arguments")); URLClassLoader classLoader = new URLClassLoader(new URL[] { new URL("file:gamepack.jar") }); Applet applet = (Applet)classLoader.loadClass("client").newInstance(); applet.setStub(stub); applet.setSize(new Dimension(763, 514)); applet.init(); applet.start(); i.add(applet); i.pack(); i.setDefaultCloseOperation(3); label.setVisible(false); } } public OSRSLoader(Pattern parameterPattern, String frameSource) { String key; String value; for(Matcher param = parameterPattern.matcher(frameSource); param.find(); System.out.println((new StringBuilder("Parameter Loaded. Key = ")).append(key).append(", value =").append(value).toString())) { key = param.group(1); value = param.group(2); parameters.put(key, value); } } private String getPageSource(URL url) throws IOException, InterruptedException { URLConnection cn = url.openConnection(); cn.addRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); cn.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); cn.addRequestProperty("Accept-Encoding", "gzip,deflate"); cn.addRequestProperty("Accept-Language", "en-gb,en;q=0.5"); cn.addRequestProperty("Connection", "keep-alive"); cn.addRequestProperty("Host", "www.runescape.com"); cn.addRequestProperty("Keep-Alive", "300"); cn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.01"); DataInputStream di = new DataInputStream(cn.getInputStream()); byte tmp[] = new byte[cn.getContentLength()]; di.readFully(tmp); di.close(); Thread.sleep(250 + (int)Math.random() * 500); return new String(tmp); } public void appletResize(int i, int j) { } public void setCodeBase(URL codeBase) { this.codeBase = codeBase; } public void setDocumentBase(URL documentBase) { this.documentBase = documentBase; } public AppletContext getAppletContext() { return null; } public URL getCodeBase() { return codeBase; } public URL getDocumentBase() { return documentBase; } public String getParameter(String name) { return (String)parameters.get(name); } public boolean isActive() { return false; } public static void Download(String world, String archive) throws Exception { URLConnection jarConnection = (new URL((new StringBuilder(String.valueOf(world))).append(archive).toString())).openConnection(); FileOutputStream out = new FileOutputStream("./gamepack.jar"); InputStream input = jarConnection.getInputStream(); byte info[] = new byte[1024]; int ln; while((ln = input.read(info)) != -1) out.write(info, 0, ln); } private static Map parameters = new HashMap(); private URL codeBase; private URL documentBase; } </code></pre> <p>Java applet code:</p> <pre><code>import java.applet.*; import java.awt.*; import java.io.DataInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLClassLoader; import java.net.URLConnection; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Main extends Applet implements AppletStub { public void init() { } public void run() throws FileNotFoundException, Exception { new Main(); } public Main() throws Exception, FileNotFoundException { String str = getPageSource(new URL("http://oldschool38.runescape.com")); Pattern pattern = Pattern.compile("gamep\\w+"); Matcher match = pattern.matcher(str); if(match.find()) { Main stub = new Main(Pattern.compile("&lt;param name=\"([^\\s]+)\"\\s+value=\"([^&gt;]*)\"&gt;"), str); Download("http://oldschool38.runescape.com/", (new StringBuilder(String.valueOf(match.group(0)))).append(".jar").toString()); stub.setCodeBase(new URL((new StringBuilder("http://oldschool38.runescape.com/")).append(match.group(0)).append(".jar").toString())); stub.setDocumentBase(new URL((new StringBuilder("http://oldschool38.runescape.com/")).append(match.group(0)).append(".jar").toString())); stub.getParameter((String)parameters.get("java_arguments")); URLClassLoader classLoader = new URLClassLoader(new URL[] { new URL("file:gamepack.jar") }); Applet applet = (Applet)classLoader.loadClass("client").newInstance(); applet.setStub(stub); applet.setPreferredSize(new Dimension(765, 503)); applet.setSize(new Dimension(765, 503)); applet.init(); applet.start(); } } public Main(Pattern parameterPattern, String frameSource) { String key; String value; for(Matcher param = parameterPattern.matcher(frameSource); param.find(); System.out.println((new StringBuilder("Parameter Loaded. Key = ")).append(key).append(", value =").append(value).toString())) { key = param.group(1); value = param.group(2); parameters.put(key, value); } } private String getPageSource(URL url) throws IOException, InterruptedException { URLConnection cn = url.openConnection(); cn.addRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); cn.addRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); cn.addRequestProperty("Accept-Encoding", "gzip,deflate"); cn.addRequestProperty("Accept-Language", "en-gb,en;q=0.5"); cn.addRequestProperty("Connection", "keep-alive"); cn.addRequestProperty("Host", "www.runescape.com"); cn.addRequestProperty("Keep-Alive", "300"); cn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.01"); DataInputStream di = new DataInputStream(cn.getInputStream()); byte tmp[] = new byte[cn.getContentLength()]; di.readFully(tmp); di.close(); Thread.sleep(250 + (int)Math.random() * 500); return new String(tmp); } public void appletResize(int i, int j) { } public void setCodeBase(URL codeBase) { this.codeBase = codeBase; } public void setDocumentBase(URL documentBase) { this.documentBase = documentBase; } public AppletContext getAppletContext() { return null; } public URL getCodeBase() { return codeBase; } public URL getDocumentBase() { return documentBase; } public String getParameter(String name) { return (String)parameters.get(name); } public boolean isActive() { return false; } public static void Download(String world, String archive) throws Exception { URLConnection jarConnection = (new URL((new StringBuilder(String.valueOf(world))).append(archive).toString())).openConnection(); FileOutputStream out = new FileOutputStream("./gamepack.jar"); InputStream input = jarConnection.getInputStream(); byte info[] = new byte[1024]; int ln; while((ln = input.read(info)) != -1) out.write(info, 0, ln); } private static Map parameters = new HashMap(); private URL codeBase; private URL documentBase; } </code></pre> <p>The error when i try and load it in html is: <code>java.lang.reflect.invocationtargetexception</code> i click details and it sais: </p> <pre><code>Java Plug-in 10.25.2.17 Using JRE version 1.7.0_25-b17 Java HotSpot(TM) Client VM User home directory = C:\Users\Will ---------------------------------------------------- c: clear console window f: finalize objects on finalization queue g: garbage collect h: display this help message l: dump classloader list m: print memory usage o: trigger logging q: hide console r: reload policy configuration s: dump system and deployment properties t: dump thread list v: dump thread stack x: clear classloader cache 0-5: set trace level to &lt;n&gt; ---------------------------------------------------- </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload