導航:首頁 > 程序命令 > javalinux常用命令

javalinux常用命令

發布時間:2022-06-03 13:16:51

java面試讓說幾個你常用的linux命令,怎麼說

照實說,告訴你幾個工作的時候常用的linux命令
cd
ls
tail -f
cat
more
ps -ef
grep
find
vi
chmod
基本就這些了,了解一下,面試說幾個就好了

② java程序執行linux命令

首先確保Linux開啟sshd服務,並支持遠程SSH連接。java程序使用jsch框架登錄Linux,執行命令。

protected void creation() throws Exception {
JSch jsch = new JSch();
session = jsch.getSession(userName, host, port);
session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setTimeout(CONNECT_TIMEOUT);
session.setConfig("PreferredAuthentications", "password,keyboard-interactive");
session.setServerAliveInterval(1000 * 60 * 2);
session.connect();
}

public String sendCommand(String command) throws Exception {
if(!isConnected())
throw new JSchException("Session is not connected, command exec faild.");
final ChannelExec exec = (ChannelExec)session.openChannel("exec");
ByteArrayOutputStream out = new ByteArrayOutputStream();
exec.setCommand(command);
exec.setOutputStream(out);
exec.setExtOutputStream(out);
exec.connect();
final Thread thread = new Thread() {
public void run() {
while(!exec.isEOF()) {
try { Thread.sleep(500L); } catch(Exception e) {}
}
}
};
thread.setDaemon(true);
thread.start();
thread.join(EXEC_TIMEOUT);
thread.interrupt();
if(thread.isAlive()) {
throw new JSchException("Exec Time Out Error");
} else {
try {
exec.disconnect();
out.close();
} catch (Exception e) {
}
byte[] lens = out.toByteArray();
String result = new String(lens, charset);
if(result.startsWith("bash") && result.indexOf("command not found") != -1)
return "";
return result;
}
}

③ java如何連接linux系統後台執行相應的命令

java提供的Runtime 這個類來執行系統命令的,用法如下:

1.得到Runtime對象。
public void execCommand(String command) throws IOException {
// start the ls command running
//String[] args = new String[]{"sh", "-c", command};
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command); //這句話就是shell與高級語言間的調用
//如果有參數的話可以用另外一個被重載的exec方法
//實際上這樣執行時啟動了一個子進程,它沒有父進程的控制台
//也就看不到輸出,所以需要用輸出流來得到shell執行後的輸出

2.得到輸入流。
InputStream inputstream = proc.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
// read the ls output
String line = "";
StringBuilder sb = new StringBuilder(line);
while ((line = bufferedreader.readLine()) != null) {
//System.out.println(line);
sb.append(line);
sb.append('\n');
}
//tv.setText(sb.toString());
//使用exec執行不會等執行成功以後才返回,它會立即返回
//所以在某些情況下是很要命的(比如復制文件的時候)
//使用wairFor()可以等待命令執行完成以後才返回
try {
if (proc.waitFor() != 0) {
System.err.println("exit value = " + proc.exitValue());
}
}
catch (InterruptedException e) {
System.err.println(e);
}
}
}

④ java程序里調用linux命令

1.Java調用shell

Java語言以其跨平台性和簡易性而著稱,在Java裡面的lang包里(java.lang.Runtime)提供了一個允許Java程序與該程序所運
行的環境交互的介面,這就是Runtime類,在Runtime類里提供了獲取當前運行環境的介面。
其中的exec函數返回一個執行shell命令的子進程。exec函數的具體實現形式有以下幾種:
public Process exec(String command) throws IOException
public Process exec(String command,String[] envp) throws
IOException
public Process exec(String command,String[] envp,File dir) throws
IOException
public Process exec(String[] cmdarray) throws IOException
public Process exec(String[] cmdarray, String[] envp) throws
IOException
public Process exec(String[] cmdarray, String[] envp,File dir)
throws IOException

我們在這里主要用到的是第一個和第四個函數,具體方法很簡單,就是在exec函數中傳遞一個代表命令的字元串。exec函數返回的是一個Process類
型的類的實例。Process類主要用來控制進程,獲取進程信息等作用。(具體信息及其用法請參看Java doc)。

1)執行簡單的命令的方法:

代碼如下:

⑤ 如何用java調用linux shell命令

用java調用linux shell命令:
String shpath="/test/test.sh"; //程序路徑
Process process =null;
String command1 = 「chmod 777 」 + shpath;
process = Runtime.getRuntime().exec(command1);
process.waitFor();
String var="201102"; //參數
String command2 = 「/bin/sh 」 + shpath + 」 」 + var;
Runtime.getRuntime().exec(command2).waitFor();

⑥ 怎麼在java中執行linux 命令 netstat

Java 可以通過 Runtime 調用Linux命令,形式如下:
Runtime.getRuntime().exec(command)
但是這樣執行時沒有任何輸出,因為調用 Runtime.exec 方法將產生一個本地的進程,並返回一個Process子類的實例

由於調用 Runtime.exec 方法所創建的子進程沒有自己的終端或控制台,因此該子進程的標准IO(如stdin,stdou,stderr)都通過 Process.getOutputStream(),Process.getInputStream(), Process.getErrorStream() 方法重定向給它的父進程了。
用戶需要用這些stream來向子進程輸入數據或獲取子進程的輸出,下面的代碼可以取到 linux 命令的執行結果:

try {
String[] cmd = new String[]{」/bin/sh」, 「-c」, 」 ls 「};
Process ps = Runtime.getRuntime().exec(netstat);
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append(」\n」);
}
String result = sb.toString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}

⑦ linux上移動目錄的命令是什麼Java中的提示的快捷鍵如何設置

linux中,mv的命令有2個用途。一個是改名,一個是移動文件或者目錄。具體用法請查閱系統手冊(man手冊),不會用就度娘好了。
另外還有cp,scp命令,也可以復制移動文件和目錄。但是和mv有區別,mv可以理解為剪切,cp可以理解為復制粘貼。

⑧ Java開發人員應掌握Linux哪些方面

1在linux配置java,tomcat環境變數,因為j2ee一般服務都是架設在linux上面的
2簡單的shell操作(相當於dos命令行),比如刪除,查找文件,進入某一個目錄基本操作等等
3會設置linux開機自啟動程序,因為mysql等服務安裝完後,開機自動啟動服務需要自己設置的
4懂得linux下的一些常用程序vi,gedit,telnet,openoffice等的常用執行命令和程序

⑨ 怎麼用java代碼運行linux命令

以下方法支持Linux和windows兩個系統的命令行調用。還用到了apache的lang工具包commons-lang3-3.1.jar來判斷操作系統類型、也用到了和log4j-1.2.16.jar來列印日誌。至於rm -rf 是否能成功刪除文件,可以手動去調用命令行試試。

privateStringcallCmd(Stringcmd)throwsInterruptedException,UnHandledOSException,ExecuteException{
if(SystemUtils.IS_OS_LINUX){
try{
//使用Runtime來執行command,生成Process對象
Processprocess=Runtime.getRuntime().exec(
newString[]{"/bin/sh","-c",cmd});
intexitCode=process.waitFor();
//取得命令結果的輸出流
InputStreamis=process.getInputStream();
//用一個讀輸出流類去讀
InputStreamReaderisr=newInputStreamReader(is);
//用緩沖器讀行
BufferedReaderbr=newBufferedReader(isr);
Stringline=null;
StringBuildersb=newStringBuilder();
while((line=br.readLine())!=null){
System.out.println(line);
sb.append(line);
}
is.close();
isr.close();
br.close();
returnsb.toString();
}catch(java.lang.NullPointerExceptione){
System.err.println("NullPointerException"+e.getMessage());
logger.error(cmd);
}catch(java.io.IOExceptione){
System.err.println("IOException"+e.getMessage());
}
thrownewExecuteException(cmd+"執行出錯!");
}

if(SystemUtils.IS_OS_WINDOWS){
Processprocess;
try{
//process=newProcessBuilder(cmd).start();
String[]param_array=cmd.split("[\s]+");
ProcessBuilderpb=newProcessBuilder(param_array);
process=pb.start();
/*process=Runtime.getRuntime().exec(cmd);*/
intexitCode=process.waitFor();
InputStreamis=process.getInputStream();
InputStreamReaderisr=newInputStreamReader(is);
BufferedReaderbr=newBufferedReader(isr);
Stringline;
StringBuildersb=newStringBuilder();

while((line=br.readLine())!=null){
System.out.println(line);
sb.append(line);
}
is.close();
isr.close();
br.close();
returnsb.toString();
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
thrownewExecuteException(cmd+"執行出錯!");
}

thrownewUnHandledOSException("不支持本操作系統");
}

⑩ 在linux 下 java命令有哪些

-h 是幫助命令,如果有java環境的話,java -h 命令會列出相關的命令

閱讀全文

與javalinux常用命令相關的資料

熱點內容
chrpythonord 瀏覽:353
android切片 瀏覽:230
前端js調用php 瀏覽:590
文件夾res是什麼 瀏覽:488
linuxput命令 瀏覽:931
智能仿生演算法模擬退火 瀏覽:903
汽車辦解壓能代辦嗎 瀏覽:12
美林程序員 瀏覽:841
安卓如何開網路 瀏覽:730
宿來app什麼時候上線 瀏覽:764
成都python培訓機構好不好 瀏覽:421
mysql查看配置命令 瀏覽:597
v8編譯cmake 瀏覽:965
app品牌起步階段需要什麼營銷 瀏覽:358
壓縮機製冷劑溫度 瀏覽:930
會日語的程序員 瀏覽:19
網銀密碼加密失敗怎麼回事 瀏覽:727
android開發音樂播放器 瀏覽:809
ug120陣列命令快捷鍵 瀏覽:597
氣動隔膜式壓縮機 瀏覽:470