Java calls Linux terminal commands

Source: Internet
Author: User
Tags gluster

To create a gluster volume after successful user registration, let's take a look at how to use Java to call Linux terminal commands.

The source code is as follows:

import java.io.InputStreamReader;import java.io.LineNumberReader;public class RuntimeTest{     public static void main(String args[]){try{ Process process = Runtime.getRuntime().exec ("ls");  InputStreamReader ir=new InputStreamReader(process.getInputStream()); LineNumberReader input = new LineNumberReader (ir); String line; while ((line = input.readLine ()) != null){  System.out.println(line);}catch (java.io.IOException e){ System.err.println ("IOException " + e.getMessage());} }}

References:

1. http://zhidao.baidu.com/question/41907875.html
2. http://www.blogjava.net/envoydada/archive/2007/05/08/115877.html
3. http://blog.csdn.net/HEYUTAO007/article/details/5705499
4. http://blog.csdn.net/bigbuiding/article/details/1675105
5. http://dangflying.blog.163.com/blog/static/8693721820111218320357/

Why can't I execute the sudo gluster command on the terminal by using Objective C? Then I try to put the gluster volume command in the shell script and use crontab to regularly execute the script, the same is found. The sudo gluster command was not executed. Then I added a line of sudo mkdir test to the script and did not execute it. Instead, I changed it to mkdir test, it indicates that the sudo command is used, that is, the user permission problem. Because the gluster command must be executed with sudo, how can we correctly execute the sudo command in the shell script? There are two solutions:
1. Switch to the root mode, that is, the superuser mode can be correctly executed.
2. How do I execute terminal commands under a Super User in normal user mode? The password is free when sudo is used to execute the command, because a common user needs to enter the password after entering the sudo command, which is the password of the current user, save the password and you can execute the superuser command.

References:

1. http://blog.sina.com.cn/s/blog_700132870100t8lj.html
2. http://bbs.chinaunix.net/thread-1916342-1-1.html

If the Linux terminal command has been successfully called in Java, it will be okay if it is placed in JSP.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.