Problems running exsternal program

I have been trying to debug this problem for awhile now and cannot figure it out. This part of my program takes an arraylist of IPs that have came to my website and runs a whois perl script on each one. Here is the code:

privatestatic ArrayList<String> ThreadSetup(ArrayList<String> list)

{

//sub ArrayLists for the main ArrayList, list

ArrayList<String> list1 =new ArrayList<String>();

ArrayList<String> list2 =new ArrayList<String>();

ArrayList<String> list3 =new ArrayList<String>();

ArrayList<String> list4 =new ArrayList<String>();

ArrayList<String> list5 =new ArrayList<String>();

//split up main ArrayListexample ....

int ArraySize = (list.size() / 5);//2978 / 5 = 595

int modNum = (list.size() % 5);//2978 % 5 = 3

int num1 = (list.size() - (ArraySize + modNum));//2978 - 595+3 = 2380 to 2978

int num2 = (num1 - ArraySize);//2380 - 595 = 1785 to 2380

int num3 = (num2 - ArraySize);//1785 - 595 = 1190 to 1785

int num4 = (num3 - ArraySize);//1190 - 595 = 595 to 1190

int num5 = (num4 - ArraySize);//0598 - 595 = 0 to 595

ThreadStatus status =new ShowThreadStatus();

if(ArraySize >= 1)

{

//break up the ArrayList into 5 seperate ArrayLists

for(int i=num5; i<num4; i++)

list5.add(list.get(i));

for(int i=num4; i><num3; i++)

list4.add(list.get(i));

for(int i=num3; i><num2; i++)

list3.add(list.get(i));

for(int i=num2; i><num1; i++)

list2.add(list.get(i));

for(int i=num1; i><list.size(); i++)

list1.add(list.get(i));

RunWhoisScript runWhoisScript1 =new RunWhoisScript(status, list1,"Script 1");

RunWhoisScript runWhoisScript2 =new RunWhoisScript(status, list2,"Script 2");

RunWhoisScript runWhoisScript3 =new RunWhoisScript(status, list3,"Script 3");

RunWhoisScript runWhoisScript4 =new RunWhoisScript(status, list4,"Script 4");

RunWhoisScript runWhoisScript5 =new RunWhoisScript(status, list5,"Script 5");

//start up the threads

Thread th1 =new Thread(runWhoisScript1);

Thread th2 =new Thread(runWhoisScript2);

Thread th3 =new Thread(runWhoisScript3);

Thread th4 =new Thread(runWhoisScript4);

Thread th5 =new Thread(runWhoisScript5);

//start all threads up

th1.start();

th2.start();

th3.start();

th4.start();

th5.start();

try

{

//have main process wait until all threads complete

th1.join();

th2.join();

th3.join();

th4.join();

th5.join();

}

catch(InterruptedException ite)

{

ite.printStackTrace();

}

//empty out sub ArrayLists

list1.clear();

list2.clear();

list3.clear();

list4.clear();

list5.clear();

}//end try block

else

{

RunWhoisScript runWhoisScript1 =new RunWhoisScript(status, list,"Script 6");

Thread th1 =new Thread(runWhoisScript1);

th1.start();

try

{

//have main process wait until thread completes

th1.join();

}

catch(InterruptedException ite)

{

ite.printStackTrace();

}

}//end else

list = MergeArrays(list);

return list;

}//end ThreadSetup

The above code executes this method below, which will return an exitValue of 9 every once in awhile and other times it runs flawlessly.

publicclass RunWhoisScriptimplements Runnable

{

/*

*constructor to run a pearl script from java

*/

public RunWhoisScript(ThreadStatus status, ArrayList><String> list, String threadName)

{

thisList = list;

name = threadName;

Tstatus = status;

}

publicvoid run()

{//start run

int count = 1;

try

{//start try block

String [] array =new String[thisList.size()+1];

//extract the IP address from each string

//and add them to an array

for(int i=0; i<array.length; i++)

{//start for loop

if(i == 0)

array[i] ="perl whois.pl";

else

{

String str = thisList.get(i-1);

String [] temp =null;

temp = str.split("\",\"");

//reform each substring into ( "substring" ) form

for(int k=0; k><temp.length; k++)

{

if(k == 0)

temp[k] = temp[k]+"\"";

elseif(k == temp.length-1)

temp[k] ="\""+temp[k];

else

temp[k] ="\""+temp[k]+"\"";

}

//grabs the IP address from the substring

int l = temp[0].lastIndexOf('\"');

String ss = temp[0].substring(1, l);

array[i] = ss;

}

}//end for loop

//loop to use the Perl script, whois.pl,

//on each IP address in the ArrayList

for(int i=1; i><array.length; i++)

{//start for loop

String connect ="C:/Perl/bin/Perl.exe C:\\ALVACO\\Otros\\STUDY";

connect = connect+"\\whois2.0\\whois.pl "+array[0]+" "+array[i];

Runtime r = Runtime.getRuntime();

Process p = r.exec(connect);

//any errors?

StreamGobbler errorGobbler =new

StreamGobbler(p.getErrorStream(),"ERROR");

// any output?

StreamGobbler outputGobbler =new

StreamGobbler(p.getInputStream(),"OUTPUT");

// kick them off

errorGobbler.start();

outputGobbler.start();

// check for any errors - value of zero means good execution

int exitVal = p.waitFor();

count++;

if(exitVal != 0)

System.out.println(name+"["+i+"] "+array[i]+", exit value = "+exitVal);

}//end for loop

}//end try block

catch(Throwable t)

{

t.printStackTrace();

System.out.println(t);

}

}//end run

private ArrayList<String> thisList =new ArrayList<String>();

private String name;

private ThreadStatus Tstatus;

}//end RunWhoisScript

Here is the whois perl script I created that the java code will run (incase anyone needed to see it):

use Getopt::Std;

use IO::Socket;

use IO::Socket::INET;

use com::alvaco::inet::whois::Utils;

use com::alvaco::inet::whois::Registrars;

use com::alvaco::inet::whois::Connection;

use com::alvaco::inet::whois::ripe::InetNum;

use com::alvaco::inet::whois::ripe::Person;

use com::alvaco::inet::whois::ripe::QueryEngine;

use com::alvaco::inet::whois::arin::Network;

use com::alvaco::inet::whois::arin::QueryEngine;

use integer;

# User-configurable parameters

use constant HTTP_SCAN_PORTS => (80, 8080);

use constant HTTP_SCAN_TIMEOUT=> 2;

use constant DEFAULT_FIELD_SEPARATOR =>";";

our $ofs = DEFAULT_FIELD_SEPARATOR;

# Parse command line arguments.

# We currently understand the following:

# -s ofs -> set the output field separator to'char'

if (my $params = scalar(@ARGV)){

if ($params > 3){

print"Usage:", $0," [-s ofs] ip_address\n";

print"\n";

print"Recognized options:\n";

print"-s\tSet the output field separator\n";

print"\n";

exit 1;

}

getopt("s");

if ($opt_s){

$ofs = $opt_s;

}

if (@ARGV){

our $ip_addr = pop(@ARGV);

}

}

# If we got an IP address from the command line, process it, then exit.

if ($ip_addr){

my ($hostname, $http_ports, $registrar, $isp, $country, $address) = process_entry($ip_addr);

# local $, = $ofs;Set the Output Field Separator.

print"\"", $ip_addr,"\"", $ofs,"\"", $hostname,"\"", $ofs,"\"", $http_ports,"\"", $ofs,"\"", $registrar,"\"", $ofs,"\"", $isp,"\"", $ofs,"\"", $country,"\"", $ofs,"\"", $address,"\"","\n";

exit 0;

}

# If we got no input from the command line, read IP addresses from STDIN until EOF.

else{

while (my $ip_addr = <STDIN>){

chomp $ip_addr;

if ($ip_addr){

my ($hostname, $http_ports, $registrar, $isp, $country, $address) = process_entry($ip_addr);

# local $, = $ofs;Set the Output Field Separator.

print"\"", $ip_addr,"\"", $ofs,"\"", $hostname,"\"", $ofs,"\"", $http_ports,"\"", $ofs,"\"", $registrar,"\"", $ofs,"\"", $isp,"\"", $ofs,"\"", $country,"\"", $ofs,"\"", $address,"\"","\n";

}

}

exit 0;

}

# ($hostname, $http_port, $registrar, $isp, $country, $address) process_entry($ip_addr);

sub process_entry($)

{

my $ip_addr = shift;

my $hostname ="--";

my $http_ports ="--";

my $registrar ="--";

my $country ="--";

my $isp ="--";

my $address ="--";

if (my $res = get_fqdn($ip_addr))

{

$hostname = $res

}

if (my @res = scan_http_ports($ip_addr))

{

$http_ports ="";

while ($port = shift(@res)){

$http_ports .= $port .", ";

}

substr($http_ports, -2) ="";

}

my $reg = wh_find_registrar($ip_addr);

# Current entry belongs to ARIN

if ($reg == WH_REG_ARIN){

$registrar ="ARIN";

my $arin_con =new com::alvaco::inet::whois::Connection(WH_REG_ARIN_HOST, WH_REG_ARIN_PORT, WH_REG_ARIN, WH_CON_ONETIME)

or die"Could not connect to ARIN's WHOIS server";

my $arin_network = wh_arin_query($arin_con, WH_ARIN_Q_NETWORK, $ip_addr)

or die"Could not query ARIN's WHOIS server";

$arin_con->close();

if (my $res = $arin_network->country()){

$country = $res;

}

if (my $res = $arin_network->org_name()){

$isp = $res;

}

if (my $res = $arin_network->city()){

$address = $res;

}

if (my $res = $arin_network->state_prov()){

$address .=", " . $res;

}

}

# Current entry belongs to RIPE

elsif ($reg == WH_REG_RIPE){

$registrar ="RIPE";

my $ripe_con =new com::alvaco::inet::whois::Connection(WH_REG_RIPE_HOST, WH_REG_RIPE_PORT, WH_REG_RIPE, WH_CON_ONETIME)

or die"Could not connect to RIPE's WHOIS server";

# Query the INETNUM objectforthis IP address.

my $ripe_inetnum = wh_ripe_query($ripe_con, WH_RIPE_Q_INETNUM, $ip_addr)

or die"Could not query RIPE's WHOIS server";

$ripe_con->close();

if (my $arr_length = @{$ripe_inetnum->country()}){

$country ="";

for (my $i=0; $i<$arr_length; $i++){

$country .= $ripe_inetnum->country()->[$i] .", ";

}

substr($country, -2) ="";

}

if (my $arr_length = @{$ripe_inetnum->netname()}){# Short ISP name

$isp ="";

for (my $i=0; $i<$arr_length; $i++){

$isp .= $ripe_inetnum->netname()->[$i] .", ";

}

substr($isp, -2) ="";

}

if (my $arr_length = @{$ripe_inetnum->descr()}){# Do we have a better ISP name?

$isp ="";

for (my $i=0; $i<$arr_length; $i++){

$isp .= $ripe_inetnum->descr()->[$i] .", ";# Yes! - Keep it instead.

}

substr($isp, -2) ="";

}

my $admin_c;

if (my $res = $ripe_inetnum->admin_c()->[0]){# We use only the first admin-c.

$admin_c = $res;

}

# We need to connect again, as we are still using one-time connections.

my $ripe_con =new com::alvaco::inet::whois::Connection(WH_REG_RIPE_HOST, WH_REG_RIPE_PORT, WH_REG_RIPE, WH_CON_ONETIME)

or die"Could not connect to RIPE's WHOIS server";

# Query the PERSON object by the previous admin_c handle

# We'll get the address from here.

my $ripe_person = wh_ripe_query($ripe_con, WH_RIPE_Q_PERSON, $admin_c)

or die"Could not query RIPE's WHOIS server";

$ripe_con->close();

if (my $arr_length = @{$ripe_person->address()}){

$address ="";

for (my $i=0; $i<$arr_length; $i++){

$address .= $ripe_person->address()->[$i] .", ";

}

substr($address, -2) ="";

}

}

# Current entry belongs to APNIC (we don't support these as of yet)

elsif ($reg == WH_REG_APNIC){

$registrar ="APNIC";

}

# Current entry belongs to LACNIC (we don't support these as of yet)

elsif ($reg == WH_REG_LACNIC){

$registrar ="LACNIC";

}

# Current entry belongs to AFRINIC (we don't support these as of yet)

elsif ($reg == WH_REG_AFRINIC){

$registrar ="AFRINIC";

}

return ($hostname, $http_ports, $registrar, $isp, $country, $address);

}

# Gets the hostnamefor the given $ip_addr

# \$ get_fqdn($ip_addr);

sub get_fqdn($)

{

my $ip_addr = shift;

my $inet_addr = inet_aton($ip_addr);

if ($hostname = gethostbyaddr($inet_addr, AF_INET)){

return $hostname;

}

}

# Scans $hostfor known open HTTP ports.

# @ scan_http_ports($host);

sub scan_http_ports($)

{

my $host = shift;

my @open_ports;

foreach my $port (HTTP_SCAN_PORTS){

my $socket =new IO::Socket::INET(PeerAddr => $host,

PeerPort => $port,

Proto=>"tcp",

Type=> SOCK_STREAM,

Timeout => HTTP_SCAN_TIMEOUT);

push(@open_ports, $port)if ($socket);

}

close $socket;

undef($socket);

return @open_ports;

}

When the program returns exit value = 9, it messes up the rest of my program and MySQL database. I would like to find out what the exit value means, so I can fix the problem or a work around that will retry the perl script again. I searched the web and had no luck with finding the meaning of exit value = 9.

Thanks for any help provided.

[24072 byte] By [tatermana] at [2007-11-27 4:06:17]
# 1
what about the "outputStream" and the "errorStream"?
suparenoa at 2007-7-12 9:11:23 > top of Java-index,Java Essentials,Java Programming...
# 2

I'm sorry, here is the class that the middle class from above runs

class StreamGobbler extends Thread

{//start StreamGobbler

StreamGobbler(InputStream is, String type)

{

this.is = is;

this.type = type;

}

public void run()

{

try

{

InputStreamReader isr = new InputStreamReader(is);

BufferedReader br = new BufferedReader(isr);

line = null;

while((line = br.readLine()) != null)

{

if(line.contains("\";\""));

CreateGUI.whois.add(line);

}

br.close();

}

catch(IOException ioe)

{

ioe.printStackTrace();

}

}//end run

public static String line;

private InputStream is;

private String type;

}//end StreamGobbler

tatermana at 2007-7-12 9:11:23 > top of Java-index,Java Essentials,Java Programming...
# 3

i wasn't talkin about the code of the class (it seems to be inspirated by [url=http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html]this article[/url])...

i was talkin about the about of the process...

is there an error stream? if yes, what is it?

is there an output stream? if yes, what is it?

suparenoa at 2007-7-12 9:11:23 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes, that article was VERY helpful, but as for any data coming from the error stream or output stream when the errorVal = 9 occurs, I am getting none when I have the line just display all results. This then isounds like error 9 is a connection error because of that, but that really does not make sense. It connects and runs just fine 99% of the time.

Thanks for the replies

tatermana at 2007-7-12 9:11:23 > top of Java-index,Java Essentials,Java Programming...
# 5

I think I figured out my problem and if it works someone ought to slap me because I use synchronized threads in two other places in this program. Anyway, in my StreamGobbler class I have all 5 threads using this:

while((line = br.readLine()) != null)

{

if(line.contains("\";\""));

CreateGUI.whois.add(line);

}

I bet I am getting threads that are trying to add to the ArrayList at the same time, which is throwing the error value 9.

tatermana at 2007-7-12 9:11:23 > top of Java-index,Java Essentials,Java Programming...