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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

darron_chapmanCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Networking

From novice to tech pro — start learning today.