?
Solved

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

Posted on 2006-07-11
10
Medium Priority
?
438 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 750 total points
ID: 17086829
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 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