aspose file tools*
The moose likes Applets and the fly likes Writing a file and executing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Applets
Bookmark "Writing a file and executing" Watch "Writing a file and executing" New topic
Author

Writing a file and executing

a elmas
Greenhorn

Joined: May 14, 2011
Posts: 13
Hi,

The applet I've written has two tasks, both are called in the same class file, AppletRunner.class.(I have a single running applet).

One is doing one line calculation with inputs and showing the result in a message box, just like a calculator.

The other involves file writing and unix exec. It writes two of the TextField inputs into files, "file1.txt", "file2.txt".
Then, this file is executed by a perl code, such as,

("some_perl_code.pl" is in the same folder with other classes.)

With Eclipse, the applet is running and executing both tasks with no problems.
The problem is when I upload it to a web page, www.thepagename.com/MyApplet/classes/,
first task still works but the task involving the file writing and unix command does not work.

I went through the security issues and tried to modify the java.policy.applet file for the file read/write and Runtime permissions:



I suspect,
1. current_directory may not work as the same on the web page? I get it by:

2. java.policy.applet file is not modified correctly?

---
By the way, on the web page I could not make it work that far but, when the unix command works, another file, "some_other_file.txt" is generated in the same classes folder by the perl code and the second task also involves sending this "some_other_file.txt" to the client via email. It all works with Eclipse, but not online.
Any suggestion of making applet for this kind of job?

Thanks in advance.
-elmas
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39548
    
  27
Fine-tuning the policy file is tricky business. Why don't you just sign the applet (and possibly run the critical code sections as privileged code)? See HowCanAnAppletReadFilesOnTheLocalFileSystem for details.


Ping & DNS - updated with new look and Ping home screen widget
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

a elmas wrote:Hi,

The applet I've written has two tasks, both are called in the same class file, AppletRunner.class.(I have a single running applet).

One is doing one line calculation with inputs and showing the result in a message box, just like a calculator.

The other involves file writing and unix exec. It writes two of the TextField inputs into files, "file1.txt", "file2.txt".
Then, this file is executed by a perl code, such as,

("some_perl_code.pl" is in the same folder with other classes.)

With Eclipse, the applet is running and executing both tasks with no problems.
The problem is when I upload it to a web page, www.thepagename.com/MyApplet/classes/,
first task still works but the task involving the file writing and unix command does not work.


Since the Applet runs on the client then your Perl script must run on the client and based on your code you need to have place it in the directory on the client pointed to by the content of the String 'current_directory' . How do you get hold of that directory on the client and how do you copy the Perl script to that directory?

If you absolutely must run the Perl script then you can avoid copying to the client by writing the script to Perl's stdin (the Process.getOutputStream()). In the article I am writing on Runtime.exec and ProcessBuilder, my draft example for this is -

Rather than have the Perl script inline as my example does, you could easily place it on your server and read it using a URLConnection and write it straight to the Perl stdin.

Note - SyncPipe is a Runnable that copies an InputStream to an OutputStream.

There are ways to avoid actually writing the two files file1.txt and file2.txt but you would possibly have to modify your Perl script. Without access to the Perl script I can't say how easy it would be.

I echo Ulf's view that modifying policy files is normally a bad idea. The idea of an Applet is that it can be run remotely so if the user has to spend some effort modifying the policy then it would make it difficult to run. Just sign the Applet jar file.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
a elmas
Greenhorn

Joined: May 14, 2011
Posts: 13
Ulf Dittmer wrote:Fine-tuning the policy file is tricky business. Why don't you just sign the applet (and possibly run the critical code sections as privileged code)? See HowCanAnAppletReadFilesOnTheLocalFileSystem for details.


Dear Dittmer,
I surrounded every critical code part with privileged access and I signed the Classes folder as sclasses.jar.
Then I unarchived the classes.jar file as the classes folder to run the applet locally with an html file. Such html file contains,


Now, I realized that also when running the applet locally with this html file, it runs but faces the same issue: first task works but the other one gives error.

I still have the same suspicions:
2. Did I sign correctly?
2.a. I created an alias and a keystore:

2.b. I signed it by jarsigner:


Thanks.

James Sabre wrote: Since the Applet runs on the client then your Perl script must run on the client and based on your code you need to have place it in the directory on the client pointed to by the content of the String 'current_directory' . How do you get hold of that directory on the client and how do you copy the Perl script to that directory?


Dear Sabre,
The current_directory gets the path where the class files and perl script are:
I assume below line returns,

current_directory = "http://www.abcd.edu/~xyzt/MyApplet/classes/packet_path/"
1.a. Is it correct? Honestly, I don't know how a file on a server is executed.

1.b. It is a good example but I can't embed the perl script into a class file because it links to some other perl codes and they link to some c codes which I collected all in the packet_path. (Actually this perl code runs a standalone software called UNAFold and I try to execute "hybrid2.pl". And the only thing I am interested in is one of the files that are created when this software terminates. So I guess it is impractical to modify the scripts or trying to fetch this file's entries by using Input/Output Streams. http://mfold.rna.albany.edu/?q=DINAMelt/software).

By the way, the applet is supposed to do this execution many times as 100,000.
I know it is quite a messy job but I will appreciate any help.
I'm studying your URLConnection link.
Thanks for the suggestions.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

a elmas wrote:
Dear Sabre,
The current_directory gets the path where the class files and perl script are:
I assume below line returns,

current_directory = "http://www.abcd.edu/~xyzt/MyApplet/classes/packet_path/"


I'm obviously missing something very basic because I really don't see how trying to use the current_directory as an HTTP URL can possibly work. Your Runtime.exec() command will only work if the value of current_directory is a directory on the client and, even with the most liberal attitude towards URLs, that could only ever be a directory on the server! Furthermore, I don't see how one can obtain an HTTP URL from that code fragment since System.getProperty("user.dir") (see note 1) must be directory on the client and even if it is a directory on the client I don't see how you Perl script gets copied to a sub--directory on the client.

Note 1 - maybe in an Applet the property System.getProperty("user.dir") is a URL on the server; I doubt if very much but I could be wrong. Even if it is a URL it does not allow Runtime.exec() to execute on the Client a Perl script located on the Server.

Sorry, I really don't understand what you are doing but I think it is nonsense. I could be wrong and look forward to being enlightened but I don't think I can help.

EDIT: I have just modified one of my demo signed Applets so that it displays the value of System.getProperty("user.dir"). With my HTTP server (Jetty) running on a separate machine to my client and using Firefox with IcedTea on Ubuntu it display my client home directory.

There is something very very wrong with your approach.
a elmas
Greenhorn

Joined: May 14, 2011
Posts: 13
James Sabre wrote: Even if it is a URL it does not allow Runtime.exec() to execute on the Client a Perl script located on the Server.

I absolutely need to execute these scripts from the server. Otherwise, if these perl scripts and c binary files for sure need to be copied to the client's computer in order to be executed then writing the web applet is nonsense and I will just distribute the jar package.

Beyond the web deployment, I tried the applet in my local folder and it has the same issue, I could not figure out yet.
When running AppletRunner.java in Eclipse the applet runs and there is no problem; however, starting it from the html file -that is in the same folder with classes folder- it runs but that second task gives error.
Any idea about this issue?
Thanks.

html file consists of:


----
Eclipse IDE for Java Developers.
MacOSX 10.6.7.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

a elmas wrote:
James Sabre wrote: Even if it is a URL it does not allow Runtime.exec() to execute on the Client a Perl script located on the Server.

I absolutely need to execute these scripts from the server. Otherwise, if these perl scripts and c binary files for sure need to be copied to the client's computer in order to be executed then writing the web applet is nonsense and I will just distribute the jar package.

As you say - nonsense! Applets run on the Client and cannot directly execute a Perl script or C program or even a Java program on the Server.

Beyond the web deployment, I tried the applet in my local folder and it has the same issue, I could not figure out yet.
When running AppletRunner.java in Eclipse the applet runs and there is no problem; however, starting it from the html file -that is in the same folder with classes folder- it runs but that second task gives error.
Any idea about this issue?
Thanks.

html file consists of:



----
Eclipse IDE for Java Developers.
MacOSX 10.6.7.


Your architecture is very very wrong. It looks to me like you need to run the Perl script on your Server using a Servlet (or some other server side active component) and then pass the results to your client. Whether or not you need an Applet depends on how you need to generate your data and what you are going to be doing with the results of the Perl script.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing a file and executing
 
Similar Threads
Loading Policy File from Applet
running an external program from servlet
Tomcat Java2 Security Issue: modifyThread and modifyThreadGroup
Using Policy File
Changing File Permission