I have a working Java app that can find the most recently created file in a folder. My end goal is to have that app on a web page so when a user opens the web page the page will cause the most recent file in a folder to open. I’ve read some tutorials from oracle on creating simple applets, but everything I’ve come across involves making a GUI that my page will not need.
Currently, when I open the html page in firefox it loads all the html except for the applet. It gives no error messages, it just doesn’t do anything. I think it’s because it isn’t recognizing my java app as an applet, so I think I may need to do more to convert my code to an applet. I added “extends Applet” to my java class name, and I looked into adding an init method, but that seems more geared towards those who want GUI’s.
The java app is below in case that may help. As far as the HTML goes, I embedded the applet as applet code=”FirstApplet” width=’300′ height=’300′ (with proper opening and closing tags) and it is located in the same folder as the java app.
import java.applet.Applet;
import java.io.File;
import java.io.IOException;
@SuppressWarnings("serial")
public class FirstApplet extends Applet{
public static File[] getPath(String folderPath){
File directory = new File(folderPath);
File[] myarray;
myarray=directory.listFiles();
return myarray;
}
public static String getMostCurr(File[] fileArray){
File mostCurrent = null;
for (int i = 0; i < fileArray.length; i++) {
if ((mostCurrent==null)||
(fileArray[i].lastModified()> mostCurrent.lastModified()))
{
mostCurrent = fileArray[i];
} }
//System.out.println(mostCurrent.toString());
return mostCurrent.toString();
}
public static void main(String[] args) throws IOException{
//opens file on MACINTOSH
Runtime.getRuntime().exec(new String[]{"/usr/bin/open",
getMostCurr(getPath("/Users/guest/Desktop/lectures/testFileReader"))});
}
}
EDIT**: Here is the HTML page as requested.
<html>
<head>
<title>My First Java Applet </title>
</head>
<body>
Here's my first java applet: <br> <br>
<applet code ='FirstApplet.class' width='300' height='300'>
</body>
</html>
Developing, debugging and deploying applets (even to one PC) is not convenient or easy. Take that from me, I have vast experience with applets.
For one machine I’d use a standard app. with a
main(), possibly launched from a shell script (e.g. .sh for OS X). Instead of the manager ‘clicking a link’ that browses to a page that automatically opens a file, they ‘run (double click?) the script’ that does the same thing. In Windows, you might have even linked directly from the HTML to a.batfile, but I doubt Apple would want to open that security hole.Also look to use
Desktop.open(File)orDesktop.edit(File)instead ofRuntime.