通过在java中模拟命令shell,如何执行文件?
我制作了一个模拟命令shell(cmd)的程序,它的工作原理与我使用线程制作的程序一样完美,因此用户可以在不阻塞shell的情况下编写新命令 但是,现在我正在尝试创建一个命令(showerlog),每当用户键入这个命令时,他应该能够看到他或她在上一次会话中试图执行的错误命令 为此,iam使用(文件处理程序)在每个新会话中创建一个新的.log文件。但是,不幸的是,iam仍然无法打开包含错误命令的最后一个日志文件。我不知道为什么 请查看我的代码并随时更正:通过在java中模拟命令shell,如何执行文件?,java,shell,cmd,command,Java,Shell,Cmd,Command,我制作了一个模拟命令shell(cmd)的程序,它的工作原理与我使用线程制作的程序一样完美,因此用户可以在不阻塞shell的情况下编写新命令 但是,现在我正在尝试创建一个命令(showerlog),每当用户键入这个命令时,他应该能够看到他或她在上一次会话中试图执行的错误命令 为此,iam使用(文件处理程序)在每个新会话中创建一个新的.log文件。但是,不幸的是,iam仍然无法打开包含错误命令的最后一个日志文件。我不知道为什么 请查看我的代码并随时更正: package witheyul; im
package witheyul;
import static com.sun.corba.se.impl.util.Utility.printStackTrace;
import com.sun.javafx.tk.FileChooserType;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static java.lang.Compiler.command;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.function.Supplier;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.lang.Runtime;
import java.util.concurrent.Executors;
import sun.awt.shell.ShellFolder;
/**
*
* @author ad
*/
public class WithEyul implements Runnable {
int x = 0;
String command;
public WithEyul(String command){
this.command = command;
}
@Override
public void run(){
List<String> input = new ArrayList<String>();
StringTokenizer tokenizer = new StringTokenizer(command);
while (tokenizer.hasMoreTokens()) {
input.add(tokenizer.nextToken());
}
ProcessBuilder pb = new ProcessBuilder(input);
// ProcessBuilder creates a process corresponding to the input command
// now start the process
BufferedReader br = null;
try {
Process proc = pb.start();
// obtain the input and output streams
InputStream is = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
br = new BufferedReader(isr);
// read what the process returned
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (java.io.IOException ioe) {
try {
System.err.println("Error");
// try {
Logger logger = Logger.getLogger("Testing");
FileHandler fh;
fh = new FileHandler("E:/MyLogFile.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
logger.info(command);
}
catch (SecurityException e){
printStackTrace();
while (command.equalsIgnoreCase("Showerrlog")){
try {
Runtime.getRuntime().exec("E:\\MyLogFile.log");
} catch (IOException ex) {
Logger.getLogger(WithEyul.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (IOException ex) {
Logger.getLogger(WithEyul.class.getName()).log(Level.SEVERE, null, ex);
}
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(WithEyul.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
我相信我对她有些不对劲,但我不确定:
Runtime.getRuntime().exec("E:\\MyLogFile.log");
但除此之外,如何修复它呢
致意..要在默认编辑器中打开文件,您不能使用
运行时.exec
,您必须使用桌面。打开
if(Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(new File("E:\\MyLogFile.log"));
}
谢谢你的快速回答,但我不知道为什么我在写(淋浴日志)时会出错!!错误2014年9月18日8:25:00 EM witheyul.witheyul运行信息:我收到的错误与我编写任何其他无效代码的错误相同,我想我这里有点错误,如果(command.contentEquals(“showerlog”)@user3526756请发布完整的堆栈跟踪****欢迎使用Java命令Shell****如果您想退出Shell,键入END并按RETURN。jsh>淋浴日志错误2014年9月18日8:34:58 EM witheyul.witheyul运行信息:淋浴日志jsh>如果我编写例如(hshedw)或任何其他无效的shell命令,我将收到相同的错误。。我想知道我是否将(淋浴日志)初始化为。。(command.contentEquals(“shourrlog”))是对还是错?
if(Desktop.isDesktopSupported()) {
Desktop.getDesktop().open(new File("E:\\MyLogFile.log"));
}