Login shared network drive with JAVA

I am trying access a shared network drive through JAVA. I am using Eclipse. Pycron for automation. I registered pycron as a service. If I am logged in to Windows XP, it works fine.  When I create jar file and automate the process for the times that I am logged off, it gives "Access is denied" exception.

The error is:

java.io.FileNotFoundException: \\servername\SERVERSHARE\FileName.csv (Access is denied)

How can I get around this error?

Is there a way to login to a network drive as if I am logging on to a JDBC drive?

Thanks
package utilities;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.mail.MessagingException;
 
public class CopyFile {
 
        protected String myFromFileDirectory;
        protected String myDestinationFileDirectory;
        
        public CopyFile(String fromFileName,String toFileName) throws MessagingException{
                Email em = new Email();
                myFromFileDirectory = fromFileName;
                myDestinationFileDirectory = toFileName;
                 try {
                         File fromFile = new File(fromFileName);
                         File toFile = new File(toFileName);
                         FileInputStream from = null;
                         FileOutputStream to = null;
                         from = new FileInputStream(fromFile);
                         to = new FileOutputStream(toFile);
                         byte[] buffer = new byte[4096];
                         int bytesRead;
                         while ((bytesRead = from.read(buffer)) != -1)
                         to.write(buffer, 0, bytesRead); // write               
                  } catch (IOException e) {
                    System.err.println(e.getMessage());
                    em.postExceptionToMe("myEmail@hotmail.com", "File Transfer Exception","myEmail@hotmail.com", e);
                  }
                 }
        public static void main(String[] args) {
                String from="D:\\FileName.csv";
                String to="\\\\serverName\\SHARENAME\\FileName.csv";
                try {
                        new CopyFile(from,to);
                } catch (MessagingException e) {
                   e.printStackTrace();
                }
        }
        }

Open in new window

nicoleheritageAsked:
Who is Participating?
 
darron_chapmanConnect With a Mentor Commented:
Ok, i'll give this a shot again... since you're using Pycron to automate your tasks, and pycron is a service...

1.  go to start, run,
2.  type services.msc,
3.  find the pycron service from the list
4.  right click on the service and click properties
5.  go to the Log On Tab
6.  choose the "This account" radio button,
7.  type in your user name and password.
8.  Click OK and see if that helps...
0
 
darron_chapmanCommented:
Oh yes, you probably will need to restart the service after making that change so it recognizes what you did... right click the service and choose stop... wait for it to stop, then right click the service and choose start
0
 
nicoleheritageAuthor Commented:
I tried your solution it worked well. Thanks a bunch!!!

I have another question though... Will I need to update the password in that service when I change my login password? Or will it reload the new pswrd by my username?

The reason I am asking is, once in every two months I am prompted to renew my pswrd because of security reasons.

Again thanks again darron_chapman!!!
0
 
darron_chapmanCommented:
you need to update it everytime you change your password... it's not dynamic...  glad it worked :) ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.