0

I need to create a folder on my remote machine. For testing purpose, I am trying to create folder on my own machine using my machine IP address. My Ubuntu machine IP address is XX.X.X.XX. I want to create a folder called E book on my home directory using the following java code snippet. But the following doesn't create a folder. Please help.

boolean folder = new java.io.File("//XX.X.X.XX/home/EBook").mkdirs();

System.out.println(folder);

tenzin
  • 47
  • 9

1 Answers1

1

You can accomplish this by executing a process in java:

BufferedReader stdError = null;
try
{
    // change the you and server accordingly
    String command = "ssh you@server \"mkdir /home/name/EBook\"";
    Process p = Runtime.getRuntime().exec(command);

    stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

    // read any errors from the attempted command
    System.out.println("Here is the standard error of the command (if any):\n");
    String s = null;
    while ((s = stdError.readLine()) != null)
    {
        System.out.println(s);
    }
}
catch (IOException e)
{
    e.printStackTrace();
}
finally
{
    if (stdError != null)
    {
        try
        {
            stdError.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
Michael Markidis
  • 4,163
  • 1
  • 14
  • 21
  • Hi Michael, thanks for the response. I did change the "you" to root and "server" to my ip address but it does not create the folder. It does not show any error and does not do anything. – tenzin May 27 '17 at 20:31
  • Try to run the ssh command outside of java first. Example, just try `ssh root@server` to see if you can access it. You may have to do some setup first to allow ssh from your machine to the remote server. I will also add to my post how to read any errors produced by running the command. – Michael Markidis May 27 '17 at 20:36
  • @MichaelMarkidis - you assumed he got ssh command installed locally which may not be. – Minh Kieu May 27 '17 at 21:20
  • @Tenzin, what is the purpose of creating folders on remote machine? Maybe there are alternatives if you can explain what you try to achive? – Minh Kieu May 27 '17 at 21:20
  • @Michael I did install ssh and ran your code with the correct ip address and path. I am trying to create cat folder. It shows below. run: Here is the standard error of the command (if any): bash: mkdir /home/name/cat: No such file or directory BUILD SUCCESSFUL (total time: 5 seconds) – tenzin May 27 '17 at 22:11
  • @Minh the purpose is the server to create a folder for each remote machine where the client at the remote machine adds its own files in the respective folder. – tenzin May 27 '17 at 22:15
  • 1
    @Tenzin, sounds like you want to use SSH to login and create folders on remote Linux machine. Try using a proper library which will give proper response if anything went wrong. Have a look at this post and see if that help https://stackoverflow.com/questions/995944/ssh-library-for-java – Minh Kieu May 29 '17 at 17:13