void assignPermissions(String srcDir,String fileName,Session session){ String command = "sudo chmod 777 " + srcDir + "/" + fileName; try { ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setCommand(command); InputStream in = channelExec.getInputStream(); // Connect to the channel channelExec.connect(); // Read the output of the command (if any) BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); // Wait for the command to finish while (!channelExec.isClosed()) { Thread.sleep(1000); } // Check the exit status of the command int exitStatus = channelExec.getExitStatus(); if (exitStatus != 0) { // Handle error System.out.println("Error executing command. Exit status: " + exitStatus); } else { System.out.println("Command executed successfully."); } // Disconnect the channel channelExec.disconnect(); } catch (Exception e) { // Handle exception e.printStackTrace(); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter