Java操作Linux工具類 引言 Linux是一種常用的操作系統,在日常開發和運維工作中,我們經常需要通過Java程序來操作Linux系統的一些工具和命令。本文將介紹如何使用Java操作Linux工具類,包
Java操作Linux工具類
引言
Linux是一種常用的操作系統,在日常開發和運維工作中,我們經常需要通過Java程序來操作Linux系統的一些工具和命令。本文將介紹如何使用Java操作Linux工具類,包括調用系統命令、執行Shell腳本、遠程連接等。
調用系統命令
Java提供了Runtime
類和ProcessBuilder
類,可以方便地調用系統命令。
使用Runtime
類執行系統命令的代碼示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class CommandExecutor {
public static void main(String[] args) {
String command = "ls -l";
try {
Process process = Runtime.getRuntime().exec(command);
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
使用ProcessBuilder
類執行系統命令的代碼示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
public class CommandExecutor {
public static void main(String[] args) {
ProcessBuilder processBuilder = new ProcessBuilder("ls", "-l");
try {
Process process = processBuilder.start();
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
執行Shell腳本
除了調用系統命令,我們還可以使用Java執行Shell腳本。
使用Java執行Shell腳本的代碼示例:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class ShellExecutor {
public static void main(String[] args) {
String command = "sh script.sh";
try {
Process process = Runtime.getRuntime().exec(command);
InputStream inputStream = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
遠程連接
有時候我們需要通過Java程序與遠程Linux服務器進行交互,可以借助SSH協議進行遠程連接。
使用Java進行遠程連接的代碼示例:
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class SSHExecutor {
public static void main(String[] args) {
String host = "192.168.0.100";
String username = "root";
String password = "123456";
int port = 22;
try {
JSch jSch = new JSch();
Session session = jSch.getSession(username, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand("ls -l");
InputStream inputStream = channel.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
channel.disconnect();
session.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
類圖
使用Mermaid語法繪制的類圖如下所示:
classDiagram
class Runtime {
+exec(String command) : Process
}
class Process {
+getInputStream() : InputStream
+getErrorStream() : InputStream
}
class InputStream {
+readLine() : String
}
class BufferedReader {
+BufferedReader(InputStreamReader in) : void
+readLine() : String
+close() : void
}
class ProcessBuilder {
+ProcessBuilder(String... command) : void
+start() : Process
}
class Channel {
+getInputStream() : InputStream
+getOutputStream() : OutputStream
}
class ChannelExec {
+setCommand(String command) : void
}
class JSch {
+getSession(String username, String host, int port) : Session
}
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。