Solved

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

Posted on 2006-07-11
10
409 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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 92

Expert Comment

by:objects
ID: 17088522
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Where to store the queries for modification of table 4 62
github account with ecipse 1 48
by zero exception 10 41
Glassfish admin console not working 1 14
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

809 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