Solved

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

Posted on 2006-07-11
10
389 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:getravi2k
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Why not just run the db's console program directly?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
create script to perform the task you want to perform and run that using Runtime.exec()
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
Comment Utility
0
 
LVL 3

Expert Comment

by:anumalas
Comment Utility
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 :)
0
 

Author Comment

by:getravi2k
Comment Utility
@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
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
0
 

Author Comment

by:getravi2k
Comment Utility
@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

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
0
 

Author Comment

by:getravi2k
Comment Utility
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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now