Solved

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

Posted on 2006-07-11
10
431 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

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

Expert Comment

by:objects
ID: 17086822
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
ID: 17086829
0
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
LVL 3

Expert Comment

by:anumalas
ID: 17088094
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
ID: 17088458
@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
 
LVL 86

Expert Comment

by:CEHJ
ID: 17088581
>>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
ID: 17098066
@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
ID: 17098084
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
ID: 17117957
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

689 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