照实说,告诉你几个工作的时候常用的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命令
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 命令会列出相关的命令