Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem while creating multiple directories using mkdir of Jsch

 
Om Prakash Bijawat
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Richard ya its working fine ...actually the prob is of File separator..

 
K. Tsang
Bartender
Posts: 3444
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we check that whether the file or folder is already exist or not..?
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3444
13
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In case you don't know where the API is click here
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic