Link to home
Start Free TrialLog in
Avatar of getravi2k
getravi2k

asked on

How to use java.lang.Runtime to execute bunch of commands (something like interactively)

hi,

I have a requirement.
i am facing problem in taking the DataBase dump from a remote m/c.
the connection will be going suddenly and hence unable to take the Database dump.

is there anyway i can execute the same by using java.lang.Runtime class to get the desired classes.

Need to execute the following steps.
a) connecting to the database: exp user/password@db
    after executing the command line it prompts for the fetch size and wheter from user, table and
    it should be exported to which dmp file etc etc.
 is there any sample code available to tackle these kind of things?


Regs,
Ravindra N
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Why not just run the db's console program directly?
create script to perform the task you want to perform and run that using Runtime.exec()
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of anumalas
anumalas

Hi

if your working on windows platform create batch file with and exucte that batch file with Runtime.exec();

if your working on unix create shell script to connect to db and to do your required operations and call that script from Runtime.exec();

sample code for unix platform

DBNAME=write your database name;
 

ORACLE_HOME=/oracle8/ora$DBNAME;
ORACLE_SID=$DBNAME\1;
PATH=$PATH:/oracle8/ora$DBNAME/bin;
ORACLE_BASE=/oracle8/ora$DBNAME;
ORACLE_TERM=vt100;
TNS_ADMIN=$ORACLE_HOME/network/admin;
ORA_NLS=$ORACLE_HOME/ocommon/nls/admin/data;
ORA_NLS32=$ORACLE_HOME/ocommon/nls/admin/data;
ORA_NLS33=$ORACLE_HOME/ocommon/nls/admin/data;
NLS_LANG="GERMAN_GERMANY.WE8ISO8859P1";
#NLS_LANG="GERMAN_GERMANY.UTF8";
ORATERMPATH=$ORACLE_HOME/forms30/admin/resource;
 
export ORACLE_HOME ORACLE_SID PATH ORACLE_BASE ORACLE_TERM TNS_ADMIN ORA_NLS ORA_NLS32 ORA_NLS33 NLS_LANG ORATERMPATH DBNAME;
 
env;
username=ur username;
password=ur password;
sqlplus $username/$password@$DBNAME ;
<!-- Write your dump related database commands here --!>
save this file as <filename>.sh

call this file from Runtime.exec("<filename>.sh");

Hope it will help :)
Avatar of getravi2k

ASKER

@CEHJ:
Why not just run the db's console program directly? Can you please explain me in full?

@objects:
Article at the below URL is amazing. I learnt lot of things about it.
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

Basic requirement:

The whole code of exporting the dump will be sitting inside a thread and when ever the DB connection fails it waits for about 5mins and tries to connect it again to retrieve a new dump which is from where it stopped.
Thru the DOS console: The following are the steps required and i WANTED TO REPLICATE the same using java.lang.Runtime.
Step 01: exp user@db
It prompts for the password. how to make the system to READ the password(Password can be a hardcoded string in the pgm itself.)
Later on you need to pass the fetch size and dump name etc etc.

If you can tell me how to make the system READ the password. Thats enough for me to achieve the goal.

Regs,
Ravindra N
>>Why not just run the db's console program directly? Can you please explain me in full?

I'm wondering why you want to do this in Java particularly? Why not just run the DB's console program - most have them. If it needs Java you can execute the same commands you would in the DB console program
@objects:
I found the below code from that site you mentioned....
try {
        // Execute command
        String command = "cat";
        Process child = Runtime.getRuntime().exec(command);
   
        // Get output stream to write from it
        OutputStream out = child.getOutputStream();
   
        out.write("some text".getBytes());
        out.close();
    } catch (IOException e) {
    }

i have a question, once you write the "some text".getBytes()... you should read the input you get and depending on the input i need to send the next command like fetch size etc etc.  How to read the input is the question.

Regs,
Ravindra N

there's a link at the bottom of the page that shows you

http://javaalmanac.com/egs/java.lang/ReadFromCommand.html?l=rel

if theres a lot of output then it should be done in a seperate thread as sicussed in the 1st link I posted
Actually i understood how to make nice use of java.lang.Runtime. and i found out another way of achieving the Goal.

Your answer is in a way make me to think for an alternate solution.