Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux commands from Java

Tags:

java

linux

Is it possbile to execute linux commands with java? I am trying to create a web servlet to allow ftp users to change their passwords without ssh login access. I would like to execute the next commands:

# adduser -s /sbin/nologin clientA -d /home/mainclient/clientA
# passwd clientA
# cd /home/mainclient; chgrp -R mainclient clientA
# cd /home/mainclient/clientA; chmod 770 .
like image 890
Sergio del Amo Avatar asked Sep 24 '08 09:09

Sergio del Amo


People also ask

How do I type commands in Java on Linux?

You will type commands in an application known as the shell. Since you're using Linux, we assume you're somewhat familiar with it. Launch a new shell. To configure Java, you will need to know which shell you are running. In case you don't know, type the following command: [wayne] ~> echo $SHELL.

How to run a Java program in Linux?

To run the java program in Linux, we need to verify if Java Development Kit (JDK) is available in the system and its version. To confirm it, type the following command: ( Javac command-line tool is used for the compilation of java programs) The Javac command tool is not available in my system.

How do I configure Java on Linux?

To configure Java, you will need to know which shell you are running. In case you don't know, type the following command: Your shell will likely be bash, tcsh, sh , or ksh . To make sure Linux can find the Java compiler and interpreter, edit your shell login file according to the shell you are using. Bash.

How do I execute commands in Linux?

All basic and advanced tasks can be done by executing commands. The commands are executed on the Linux terminal. The terminal is a command-line interface to interact with the system, which is similar to the command prompt in the Windows OS.


3 Answers

Check out this.

However, doing what you are talking about is way outside spec, and I wouldnt reccommend it. To get it to work you are going to either run your app server as root, or use some other mechanism to give the user the app server is running as permission to execute these privileged commands. One small screw-up somewhere and you are "owned".

like image 72
Craig Day Avatar answered Oct 06 '22 02:10

Craig Day


Use:

Runtime.getRuntim().exec("Command");

where Command is the command string you want to execute.

like image 45
Josh Moore Avatar answered Oct 06 '22 02:10

Josh Moore


If you invoke those commands from Java, make sure to pack multiple commands to a single shell-script. This will make invocation much easier.

like image 24
Vilmantas Baranauskas Avatar answered Oct 06 '22 00:10

Vilmantas Baranauskas