3 Replies Latest reply: Oct 17, 2013 4:49 PM by blue72TA RSS

    Java Program to copy file from one directory to another failing

    blue72TA

      Hello All,

       

      Oracle Applications 12.1.3

      RDBMS: 11.2.0.30

      SQL*Plus: 10.1.0.5.0

       

      Java beginner here so help is much appreciated.  I'm have some java code that I'm using to copy a file from one directory to another in an Oracle Applications Server.  I notice that when moving to a new instnace I started to get file not found errors.  I think it's because of the directory.  My question is, does the directory in which I trying to pick up the file have to be a DBA_DIRECTORY, or a UTL_FILE directory in order for the java to find the file and move it?

       

      Here is my code...

       

      CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "COPY_FILE_JVS" AS

      import java.io.*;

      public class COPY_FILE_JVC{

        public static void copy(String SourceFile, String DestDir) {
          File source = new File(SourceFile);
          File dest = new File(DestDir);
          File cpDestFile = null;
          try {
            if (dest.isDirectory()) {
                if(source.isFile()) {
                  System.out.println("File name = " + source.getName());
                  System.out.println("File name dest = " + dest.getPath() );
                  System.out.println("File name cpDestFile = " + dest.getPath() + File.separator + source.getName());
                  cpDestFile = new File(dest.getPath() + File.separator + source.getName());
                }
                else {
                  System.out.println("Directory or File Not Found");
                  return;
                }
            }
            BufferedInputStream  br = new BufferedInputStream (new FileInputStream (source));
            BufferedOutputStream  bw = new BufferedOutputStream (new FileOutputStream (cpDestFile));
            int read = 0;
            while((read = br.read()) != -1) {
              //System.out.println((char)read);
              bw.write(read);
            }
            br.close();
            bw.close();
          }
          catch (FileNotFoundException e) {
          e.printStackTrace();
          }
          catch (IOException e) {
          e.printStackTrace();
          }
        }
      }

        • 1. Re: Java Program to copy file from one directory to another failing
          rp0428
          My question is, does the directory in which I trying to pick up the file have to be a DBA_DIRECTORY, or a UTL_FILE directory in order for the java to find the file and move it?

          No - Java exists independently of any database. Java does need the privileges on the file system though.

           

          You already have 'System.out.println' statements in the code. All you should have to do is examine them to see what the problem is.

          • 2. Re: Java Program to copy file from one directory to another failing
            blue72TA

            I get these messages when calling the java from SQL Plus as follows...

             

            serveroutput on size 100000;

            dbms_java.set_output(1000000);
            MY_PKG.CopyFile('/home/my_user/set_env.sh','/interface/home'); (this is call to oracle pl/sql package which calls the java.)

             

            PL/SQL procedure successfully completed.

            Directory or File Not Found

            PL/SQL procedure successfully completed.

             

            If I change the directory from /home/my_user/ to any directory that is defined in DBA_DIRECTORIES or is a UTL_FILE directory to program works.  Is it perhaps because the java is in a PL/SQL package as seen below?  The PL/SQL program runs as the APPS user and I have issued the following the command grant JAVASYSPRIV to APPS.

             

            Here is the PL/SQL Package....

             

            CREATE OR REPLACE PACKAGE BOLINF.MY_PKG AUTHID CURRENT_USER AS

            CopyFile(p_SourceFile IN VARCHAR2, p_DestDir IN VARCHAR2);

             

            +++++++++++++++++++++++++++++

             

            CREATE OR REPLACE PACKAGE BODY BOLINF.MY_PKG  is

            CopyFile(p_SourceFile IN VARCHAR2, p_DestDir IN VARCHAR2)

            AS LANGUAGE JAVA NAME 'COPY_FILE_JVC.copy(java.lang.String, java.lang.String)';

             

             

            • 3. Re: Java Program to copy file from one directory to another failing
              blue72TA

              I added some debug statements as follows and I get the following messages below.  Looks like the directory check is fine, it just can't read the file which has full permissions.  Can anyone help me with more meaningful debug statements to tell exactly what's happening??

               

              try {

              if (dest.isDirectory())

              System.out.println("Directory okay! = " + source.getName());

              {

              if(source.isFile()) {

              System.out.println("File name = " + source.getName());

              System.out.println("File name dest = " + dest.getPath() );

              System.out.println("File name cpDestFile = " + dest.getPath() + File.separator + source.getName());

              cpDestFile = new File(dest.getPath() + File.separator + source.getName());

              }

              else {

              System.out.println("Directory or File Not Found");

              return;

              }

               

              Connected to:
              Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
              With the Partitioning, OLAP, Data Mining and Real Application Testing options


              PL/SQL procedure successfully completed.

              Directory okay! = set_env.sh
              Directory or File Not Found

              PL/SQL procedure successfully completed.