Reputation: 61
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>
Upvotes: 1
Views: 1321
Reputation: 168825
It is HTML just for convenience.
Developing, debugging and deploying applets (even to one PC) is not convenient or easy. Take that from me, I have vast experience with applets.
It's only going to be run on my manager's computer who has all the necessary files.
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 .bat
file, but I doubt Apple would want to open that security hole.
Also look to use Desktop.open(File)
or Desktop.edit(File)
instead of Runtime
.
Upvotes: 0
Reputation: 13374
Is you applet only run when the page is viewed locally -- i.e. it is HTML just for convenience?
Because otherwise, you have a high-level design problem here. When an applet runs in a browser, it is running client-side. So it won't be able to list the files on the server. The Applet sandbox will prevent you from listing the client-side files, too. You certainly cannot perform a Runtime.exec(...)
within an applet.
You need to look into server-side technology. Or if you insist on using Applets, you will need to somehow fetch all the files on the serverside, look at the headers to figure out when each resource was created/last modified, then choose the appropriate one...
Upvotes: 2