1

I need to run my program as root. I looked at this question and it works somewhat. I need to use stdin and using the bash script doesn't seem to accept stdin. I am using Eclipse and Ubuntu. Any ideas? This also causes problems for other Java programs as they don't accept stdin either.

If you run this code:

import java.io.IOException;

public class Test {
    public static void main(String[] args) {
        while (true) {
            try {
                System.out.print((char) System.in.read());
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

And they you type stuff into the Console and hit enter, it outputs what you typed in.

When I use the bash script:

#!/bin/bash
# file:  /usr/lib/jvm/java-8-oracle/bin/java
# descr: Starter for jdk. Runs jdk as root when 
#        cmd-line-arg "--run-as-root" is specified.
#
jre="/usr/lib/jvm/java-8-oracle/bin/java.ori"
run_as_root=false
args=

# Filter command-line argument
for arg in "$@"
do
  case "$arg" in
  --run-as-root)  run_as_root=true
                  ;;
  *)              args="$args $arg"
                  ;;

  esac
done

# Remove leading whitespaces
args=$(echo $args | sed -e 's/^[ \t]*//')

if $run_as_root
then
  echo "WARNING: Running as root!"
  gksu "$jre $args"
else
  $jre $args
fi

I get a bunch of weird characters and when I remove the char cast, I get -1, meaning end of stream.enter image description here

Community
  • 1
  • 1

1 Answers1

0

Eclipse launches the program if you are running it from within Eclipse. This means you need to be logged in as root, or you need to reconfigure eclipse to use sudo.

Logged in as root is easier, as it doesn't require any extra work; however, it is also riskier, as you expose your system to both your mistakes in the program and eclipse's mistakes in it's program.

Running under sudo means that you might not always have a console. That will be even more true after you install your program. I suggest that you do the work now to just have it log to a file, as if you are dealing with sub-1000 port sockets, odds are you are writing a daemon of sorts, and it will be (after finished) routinely launched in environments that lack consoles.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • My program interacts with serial, but I do not have that serial connection open at all times, so it is easier to use stdin through the console in eclipse. When it is in production, I will be going strait to serial and will not be using stdin at all. – Christopher Smith Oct 26 '14 at 17:34
  • @ChristopherSmith Ok, so you are using serial ports, and not network ports. The main point on Linux is the same. There are programs that have consoles and programs that don't. If you want to launch without a console, you can't use stdin, stderr, and stdout. If you use commands like `sudo`, `/bin/sh`, etc, you sometimes get processes that lack consoles. Best to write your program to not assume stdin if you want to launch it via wrappers which might not provide consoles. Either that, or best to run the program logged in as root (interactive sessions typically have consoles). – Edwin Buck Oct 26 '14 at 17:55
  • This is not a daemon, the user is running the command, 'sudo java '. It has a console, this is either Eclipse's, cmd.exe or one of Linux's. Production execution is not an issue, just the part when running it with Eclipse. I need stdin with Eclipse and the program being run as root. – Christopher Smith Oct 26 '14 at 18:09
  • @ChristopherSmith Again, like in my last comment. It doesn't matter if it is a daemon or not, it matters if you are going to launch it with `sudo` as launching it with `sudo` _in many cases can leave you without a console_. – Edwin Buck Oct 26 '14 at 18:23
  • Why do most cases leave me without a console? It gives me output when I type 'sudo java' into a terminal window. – Christopher Smith Oct 26 '14 at 18:30
  • Because if the program is ran without a person logging in to run it, odds are it won't have a console. `sudo` may or may not fully emulate an interactive shell, depending on options, the depth of the nested /bin/sh commands, the options to those, etc. If you want to make it robust in launching wrapped by other stuff, you need to eventually get used to the idea that supporting a console doesn't permit any combination of "X being nested in a wrapper Y" as some of those wrapper arrangements can mess up even having a console, much less getting input into the right nesting level. – Edwin Buck Oct 26 '14 at 18:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63673/discussion-between-christopher-smith-and-edwin-buck). – Christopher Smith Oct 26 '14 at 18:40
  • If I run my program without a user logged in, I will get a java.awt.HeadlessException. I will not even get to the part where I need serial. – Christopher Smith Oct 26 '14 at 18:49