6

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 .
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Sergio del Amo
  • 76,835
  • 68
  • 152
  • 179

5 Answers5

9

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".

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Craig Day
  • 2,525
  • 1
  • 24
  • 26
5

Use:

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

where Command is the command string you want to execute.

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Josh Moore
  • 13,338
  • 15
  • 58
  • 74
1

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

Vilmantas Baranauskas
  • 6,596
  • 3
  • 38
  • 50
0

The java RunTime object has exec methods to run commands in a separate process

Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Chris
  • 230
  • 5
  • 16
0

have a look at java.lang.Runtime

Andreas Kraft
  • 3,754
  • 1
  • 30
  • 39