jQuery in Action, 2nd edition*
The moose likes Java in General and the fly likes problem while creating multiple directories using mkdir of Jsch Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem while creating multiple directories using mkdir of Jsch" Watch "problem while creating multiple directories using mkdir of Jsch" New topic
Author

problem while creating multiple directories using mkdir of Jsch

Om Prakash Bijawat
Greenhorn

Joined: Feb 01, 2013
Posts: 10
Hi,


I am getting an exception "2 : No such File" while calling mkdir function of ChannelSftp class of Jsch Library.


At the time of calling a function i am passing value "testing/FirstFolder" but it doesn't create a folder inside a folder and gives me an exception.

If I create a single folder then it will be created successfully like For Eg: if i give mkdir("testing"); then it will create "testing" folder on ftp successfully but just after this if I call mkdir("testing"+File.separator+"FirstFolder"); then it gives me an exception of No such file.




Please help me to get out of this problem...

Thanks
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

To create the folder "testing/FirstFolder" the folder "testing" must exist. You need to use
BUT - you will get an exception if either of the folders already exists. You can either ignore it if you are sure the file is actually a directory or you can use use the stat() method to look at the attributes.

P.S. I don't actually get a "No such File" exception but I don't have access to your code to see what I am doing differently.
Om Prakash Bijawat
Greenhorn

Joined: Feb 01, 2013
Posts: 10
Thanks Richard ya its working fine ...actually the prob is of File separator..

K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2234
    
    7

I usually check if the file or folder exists before creating it, else skip the mkdir and do the ftp or copy or whatever with the files.

K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

Om Prakash Bijawat wrote:Thanks Richard for you reply...

But this is not working for me...
Here is my code

Jsch jsch =new JSch();

Session session = jsch.getSession(userName,host);

session.setPassword(password);

java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications", "password");

session.setConfig(config);
session.connect();


Channel channel = session.openChannel("sftp");
ChannelSftp sftp = (ChannelSftp) channel;


sftp.connect();

System.out.println("Pwd : "+sftp.pwd());



But that code does not try to make a directory! And what does not work? What are the symptoms of 'does not work' ? Please please please tell the whole story or I am wasting my time.

Edit: your edit pulled the rug from under my feet!
Om Prakash Bijawat
Greenhorn

Joined: Feb 01, 2013
Posts: 10
How can we check that whether the file or folder is already exist or not..?
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1035
    
  10

Om Prakash Bijawat wrote:How can we check that whether the file or folder is already exist or not..?


You need to look at the Javadoc! There is a method stat() on sftp channel!
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2234
    
    7

In case you don't know where the API is click here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Om Prakash Bijawat wrote:actually the prob is of File separator..

Makes sense. File.separator is the file separator on your own machine. It's not necessarily the same as on the remote machine. If you're on a Windows machine it usually isn't.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem while creating multiple directories using mkdir of Jsch
 
Similar Threads
Transaction Timeout Exception in cmp
How to use the mkdir function
No Permission Exception
eclipse ~~compile java file question
java.util.zip : Unzipping gives Windows error