Login shared network drive with JAVA

Posted on 2008-11-13
Last Modified: 2013-12-19
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: \\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?

package utilities;






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 = != -1)

                         to.write(buffer, 0, bytesRead); // write               

                  } catch (IOException e) {


                    em.postExceptionToMe("", "File Transfer Exception","", 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) {





Open in new window

Question by:nicoleheritage
    LVL 19

    Accepted Solution

    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...
    LVL 19

    Expert Comment

    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

    Author Comment

    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!!!
    LVL 19

    Expert Comment

    you need to update it everytime you change your password... it's not dynamic...  glad it worked :) ...

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    755 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now