import java.applet.Applet; import java.awt.Graphics; import java.io.*; import java.awt.Color; public class chatbox extends Applet { public String test() { setBackground(Color.white); String fileName = System.getProperty("user.home") + System.getProperty("file.separator") + "newfile"; String msg = "This message was written by a signed applet!!!\n"; String s ; try { FileWriter fos = new FileWriter(fileName); fos.write(msg, 0, msg.length()); fos.close(); s = new String("Successfully created file :" + fileName); } catch (Exception e) { System.out.println("Exception e = " + e); e.printStackTrace(); s = new String("Unable to create file : " + fileName); } return s; } public void paint(Graphics g) { g.setColor(Color.blue); g.drawString("Signed Applet Demo", 120, 50); g.setColor(Color.magenta); g.drawString(test(), 50, 100); } }
Running this in regular java it works. RUnning it in an applet it fails. test() for example is where it should show a string of the cache path.
System.getProperty("user.home") fails in general when requesting it.
Is there any way to still do this without it failing. Like a way to ask users permission to run.