简体   繁体   中英

How to create a file / directory in remote machine using Java11

I would like to know how to create a file / directory in remote machine using Java11 ?

I did try to use:

process = Runtime.getRuntime()
         .exec("ssh root@" + hostname + " 'mkdir -p "+mdbDir+"'")
         .wait() or waitFor();

But i am getting an exception even though i use wait()

java.lang.IllegalThreadStateException: process has not exited

Please let me know what can be done.

You should only call Process.waitFor() without Process.wait() . Object.wait is about synchronization. It has nothing to do with process management.

You have a good example here about how to use an SSHClient from sshj, more specific for the task that you want to do:

Send ssh command from Java code

Moreover, you will probably need a trusted connection with the target server. Check if an SSH connection from your system requires a password, if your system and the server has a trusted connection it will no ask for the password.

If you still want to use your approach without a library I guess that the problem is that the execution is waiting for some input like adding the host to the knowhosts or the password, and that is the reason for the process not exiting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM