• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Capturing error code

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I consider myself a novice, but I think my problem belongs in this forum. The following java code (that I did not write) is stored in an Oracle database as a stored procedure and is called from a web-based application. The main use for this code is to copy files from a server to another server. It works just fine, however, I really need to capture the error code (rc). The way it is coded now, I can only see the message if I run it online in sqlplus. After reading other postings,here is my understanding that because the processing is done in the main method and it is performing operating system calls, you cannot capture an error code. Can someone verify this for me, or give me direction on how to change this so that I can capture the code.
Thanks!
import java.util.*;
import java.io.*;
public class ExecOSCmd {
public static void main (String args[])
{
int rc = 0;
if (args.length < 1) {
System.out.println("USAGE: java ExecOSCmd \'cmd\' ");
System.exit(1);
}
try {
String cmd = args[0];
FileOutputStream fos = null;
if (args.length == 2 ) {
fos = new FileOutputStream(args[1]);
}
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
CaptureStream err = new CaptureStream(p.getErrorStream(), "ERR");
Thread e = new Thread(err);
/* NOTE we do not join the error thread. If there was no
Error it may never return. We would wait forever for it to
return thus janging this process */
e.start();
CaptureStream out = new CaptureStream(p.getInputStream(),
"OUT", fos);
Thread o = new Thread(out);
o.start();
try {
rc = p.waitFor();
/* Handle exceptions for waitFor() */
} catch (InterruptedException intexc) {
System.out.println("Interrupted Exception on waitFor: " +
intexc.getMessage());
}
if (fos !=null) {
o.join(); // need to wait for the output to finish.
fos.flush();
fos.close();
}
System.out.println("ExitValue: " + rc);
} catch (Throwable t) {
System.out.println("ExitValue: " + rc);
t.printStackTrace();
}
}
}
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will move this to the Oracle forum and see if they have any ideas.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For adding code to posts, there is a button under Add Reply called Code. This will keep all your indentation of code in the same place to make it more readable.
I will look at the code and see if I can help at all. But you can also do a search at technet.oracle.com
But here are my comments. You do not have to have the code in a main method, like you do. You can create the JavaBean to have one method that returns a number or a String, or anything you wish. Then in the code you return the error code, or message or whatever you want that matches the signature of the method.
So where you have your System.out.println you could have a return statement to return it.
Now the issue becomes changing the calls to theis procedure to handle the return value. I would assume since it doesn't return any values that the call to it is not being assigned to a variable of some sort.
Hope that helps
Mark
 
m avalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. I tried to recode it as you said, but still could not get it to return a value. From other postings, I thought that it was because it was an operating system call. I will look at the recode again.
On Metalink, Oracle says that they do not support this code, it is given as just a guide.
 
m avalla
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fyi...looks like my main problem was that I was using a procedure call spec instead of a function call spec with the recode. Still have not gotten it to work correctly, but making progress.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic