Advertisement

04.01.2008 at 11:10AM PDT, ID: 23286697
[x]
Attachment Details

Using Java's Runtime.exec to call a Perl script

Asked by stockblocks in Java Servlets, Perl Programming Language, CGI Scripting

Tags:

I presume this is a simple one for a Java expert out there...

 I'm one of the fortunate billions who have a shared Linux hosting account with GoDaddy.  I have a system of Java servlets that work just fine, except the point at which one tries to create a text file to a folder. I have since found that GoDaddy limits Java servlets to write files to the server's /tmp folder only.  Fortunately, languages such as PHP or Perl are allowed to write files anywhere in the hosting account's folder tree. So I've come up with a system wherebye my Java servlet writes a file to /tmp, and then calls a Perl script to move that file to where I want it to go.  This involves the Runtime class, which I need help on.  First, here's what my Perl script looks like...

-----------begin---------------
#!/usr/bin/perl

use CGI;

$q = new CGI;

$oldfile = $q->param('from');
$newfile = $q->param('to');  

rename $oldfile, $newfile;
------------end---------------

Note the 'from' and 'to' tokens.  Can they still be used in this context?  I've always used Perl from HTML forms, so my knowledge is limited.

On the Java side, I've attached my code that's reached after my servlet has written a file "testwrite.txt" to the server's "/tmp" folder.  Of particular concern is how I specified strings in cmdLine[].

The code compiles, but I'd like to know if the way I'm using these calls is valid.


Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
// Move file from /tmp to /cgi/filetest
  final String homeDir = "/home/content/x/y/z/xyzman/html";
  Runtime rt = Runtime.getRuntime();
  String cmdLine[] = { "/usr/bin/perl", 
                       homeDir + "/cgi/supporttools/filemove.cgi",
                       "from=/tmp/testwrite.txt",
                       "to=" + homeDir + "/cgi/filetest/testwrite.txt"
                     };
  Process moveIt = rt.exec( cmdLine );
      
  // Make sure the process exits...
  try { 
    moveIt.waitFor(); 
  }
  catch (InterruptedException e) {          
    myLogWrite( e.getMessage() );
  }             
             
  int perlExitVal = moveIt.exitValue();
    
  BufferedReader reader = new BufferedReader(new InputStreamReader(moveIt.getInputStream()));
  String perlResponse = reader.readLine();
  myLogWrite( "perlResponse=" + perlResponse );
   
  myLogWrite( perlExitVal==0 ? "success" : "failed" );
[+][-]04.01.2008 at 12:09PM PDT, ID: 21257058

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.01.2008 at 12:23PM PDT, ID: 21257203

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.01.2008 at 12:39PM PDT, ID: 21257352

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: Java Servlets, Perl Programming Language, CGI Scripting
Tags: Java, Perl
Sign Up Now!
Solution Provided By: Adam314
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.01.2008 at 01:01PM PDT, ID: 21257590

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.02.2008 at 06:26AM PDT, ID: 21262758

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628