How to Put a Shell Code Into Java
Shell code typically calls a rich variety of system utilities available from the command-line prompt (also called the "shell"). Available utilities depend on the operating system; they perform functions such as monitoring the state of network connections and searching for the files that contain a given character string. When a Java program needs a function provided by an external utility, calling that utility is preferable to attempting to reinvent the wheel by implementing the function from scratch in Java. You can run shell code on your Java program to leverage the power of external utilities.
Instructions
-
-
1
Include the following lines at the beginning of your Java code:
import java.io.*;
import java.util.*;
-
2
Create a run-time context (in principle, with the same environment settings of the one where your Java application is already running) to run the shell code, as in the following sample code:
Runtime shellRuntime = Runtime.getRuntime();
Shell code will run as a separate process.
-
-
3
Launch the shell code as in the following sample code:
Process shellProcess = shellRuntime.exec("\"c:/system32/ipconfig renew\"");
Replace "c:/system32/ipconfig renew" with the full shell command you want to run, that is, the full path to the executable file followed by all necessary parameters.
-
1