[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Login shared network drive with JAVA

Posted on 2008-11-13
4
Medium Priority
?
2,053 Views
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:

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

0
Comment
Question by:nicoleheritage
  • 3
4 Comments
 
LVL 19

Accepted Solution

by:
darron_chapman earned 2000 total points
ID: 22950691
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
 
LVL 19

Expert Comment

by:darron_chapman
ID: 22950827
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
 

Author Comment

by:nicoleheritage
ID: 22951254
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
 
LVL 19

Expert Comment

by:darron_chapman
ID: 22951810
you need to update it everytime you change your password... it's not dynamic...  glad it worked :) ...
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Loops Section Overview

872 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