...
Every Java application has a single instance of class Runtime
that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the Runtime.getRuntime()
method. The semantics of Runtime.exec
are poorly defined, so it's best not to rely on its behavior any more than necessary. It will invoke the command directly without a shell. If you want a shell, you can use /bin/sh
, -c
on POSIX or cmd.exe
on Windows. The variants of exec()
that take the command line as a single String string, split it with using a StringTokenizer
. On Windows, these tokens are concatenated back into a single argument string somewhere before being executed.
...
Code Block | ||
---|---|---|
| ||
// ...
String dir = null;
int number = Integer.parseInt(System.getproperty("dir")); // only allow integer choices
switch (number) {
case 1:
dir = "data1"
break; // Option 1
case 2:
dir = "data2"
break; // Option 2
default: // invalid
break;
}
if (dir == null) {
// handle error
}
|
...
This solution can quickly become unmanageable if you have many available directories. A more extensible scalable solution is to read all the email addresses from a properties file into a java.util.Properties
object or use . Alternately, the switch statement can operator on an enum
.
Compliant Solution (Avoid Runtime.exec()
)
...