1

My Requirement is, I have one Oracle database server(Linux) and one File Server(Linux). Using Java, I have to connect to the File server and take a copy of exp dump from the database server.

Below is the piece of code written to accomplish the same but getting the error "bash: exp: command not found". Someone help me to execute exp command using Java. Thanks for all your efforts and help.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public static boolean exportDataBase ()
{
 String exportCommand = " exp UserName/Password@DB_SID file=DB_METADATA.dmp ROWS=n statistics=none log=DB_METADATA_exp.log Owner=USER1 ";
            try {
                java.util.Properties config = new java.util.Properties();
                config.put("StrictHostKeyChecking", "no");
                JSch jsch = new JSch();
                Session session = jsch.getSession(ftpUserName, ftpServer, 22);
                session.setPassword(ftpPassword);
                session.setConfig(config);
                session.connect();
                System.out.println("Connected");
                Channel channel = session.openChannel("exec");
                ((ChannelExec) channel).setCommand(exportCommand);
                channel.setInputStream(null);
                ((ChannelExec) channel).setErrStream(System.err);
                InputStream in = channel.getInputStream();
                channel.connect();
                byte[] tmp = new byte[1024];
                while (true) {
                    while (in.available() > 0) {
                        int i = in.read(tmp, 0, 1024);
                        if (i < 0)
                            break;
                        System.out.print(new String(tmp, 0, i));
                    }
                    if (channel.isClosed()) {
                        System.out.println("exit-status: " + channel.getExitStatus());
                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (Exception ee) {
                    }
                }
                channel.disconnect();
                session.disconnect();
                System.out.println("DONE");
            } catch (Exception e) {
                e.printStackTrace();
            }
}
Itok
  • 23
  • 1
  • 6
  • Have you tried to connect to the server via ssh as the user in your code and execute the command in a terminal? – brass monkey Aug 07 '18 at 15:54
  • Yes, Able to connect using puttly and excited exp command successfully., In above mentioned code tried with export command as pwd, which successfully printed working directory in console. Where as when I tried with above mentioned command getting "bash: exp: command not found" error – Itok Aug 07 '18 at 19:06
  • Where is the exp command stored on the remote system, and how does that directory get added to your command PATH on the remote system? – Kenster Aug 07 '18 at 19:57
  • 1
    Finally I got it fixed. Some how Path variables of login user are not taking while executing commands. So, when I added exp command location, it worked. Thanks for all your efforts. – Itok Aug 11 '18 at 16:37

0 Answers0