简体   繁体   English

从Java程序写入终端

[英]Writing to a terminal from a Java program

I wanted to write a java programm that open some terminals which executes in each terminal "cd go/to/a/specitic/folder". 我想写一个Java程序来打开一些终端,该终端在每个终端“ cd go / to / a / specitic / folder”中执行。 (I don't want to execute a batch/script/shell file.) (我不想执行批处理/脚本/ shell文件。)

It would be great if these could be plattform independent. 如果这些平台可以独立于平台,那就太好了。 Currently I´m working with a mac. 目前,我正在使用Mac。

I searched the web for the hole weekend and do x tries with netbeans, but there wasn't a positive result. 我在网上搜索了一个周末的漏洞,并尝试使用netbeans,但没有得到积极的结果。 One close try, I found was here http://www.coderanch.com/t/532229/java/java/Writing-terminal-Java-program . 我进行了一次仔细的尝试,发现这里是http://www.coderanch.com/t/532229/java/java/Writing-terminal-Java-program

The best codes I tried were: 我尝试过的最佳代码是:

public class NewClass1 {

public static void main(String[] args) {

    try {
        Desktop.getDesktop().open(new File("/Applications/Utilities/Terminal.app"));
    } catch (IOException ex) {
        Logger.getLogger(StartDevelop.class.getName()).log(Level.SEVERE, null, ex);
    }
    // opens a terminal but no comments / parameters could be used   
}    
}

an other try was by changing all code in the main section through: 另一种尝试是通过以下方式更改主要部分中的所有代码:

    ProcessBuilder builder = new ProcessBuilder( 
            "/Applications/Utilities/Terminal.app/Contents/MacOS/Terminal");
    builder.redirectErrorStream(true);

    try {
        String command = "ls -lai";
        Process process = builder.start();

        BufferedReader read = new BufferedReader(new InputStreamReader(
                process.getInputStream()));
        String s;

        while ((s = read.readLine()) != null) {
            System.out.println(s);
        }
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        System.out.println(e1);
    }
    //open a terminal but command is executed in terminal

and an other alternative: 另一个选择:

    try {
        String command = "ls -lai";
        Process process = Runtime.getRuntime().exec(command);
        BufferedReader read = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String s;
        while ((s = read.readLine()) != null) {
            System.out.println(s);
        }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        System.out.println(e1);
    }
    // works with output in netbeans
    // but no terminal is opening

I tried also to use an Outputwriter: 我也尝试使用Outputwriter:

 BufferedWriter outWrite = new BufferedWriter(
                new OutputStreamWriter(process.getOutputStream(), command));

 outWrite.flush();
 outWrite.newLine();

but with no effect. 但没有效果。

Thanks in advance. 提前致谢。

Cupido, 库比多

you need "SSH for Java". 您需要“ SSH for Java”。

Give a try Ganymed SSH-2 for Java 试试Java的Ganymed SSH-2

It provides even more than you need. 它提供的功能甚至超出了您的需求。

Here is a list of other Java SSH libraries 这是其他Java SSH库的列表
http://linuxmafia.com/ssh/java.html. http://linuxmafia.com/ssh/java.html。

I would suggest the Ganymed SSH because it's widely used and under BSD licence (you don't have to open-source your code). 我建议使用Ganymed SSH,因为它已被广泛使用并且已获得BSD许可(您不必开源代码)。

Perhaps you would want a better control over login/password information. 也许您希望更好地控制登录名/密码信息。 It's a bad idea to hardcode logins and passwords in Java code for a two reasons: 用Java代码对登录名和密码进行硬编码是一个不好的主意,这有两个原因:
- passwords shouldn't be stored in clear text (everyone who gets your program - gets access to your servers. Even compiled Java classes will contain clear strings for login and password. It isn't what you want) -密码不应以明文形式存储(每个获得程序的人都可以访问您的服务器。即使编译的Java类也将包含用于登录名和密码的明文字符串。这不是您想要的)
- you don't want to change your Java program if passwords were changed -如果更改了密码,您不想更改Java程序

You can generate key pairs on hosts where you want to connect to, then exchange the keys. 您可以在要连接的主机上生成密钥对,然后交换密钥。 More about this here . 更多关于这里 As result, you will not be asked about login and passwords for hosts you configured. 结果,将不会询问您所配置主机的登录名和密码。

Good luck. 祝好运。

If you don't need interactive access to the terminal window you could dynamically create a shell script and use xterm to run the script. 如果不需要交互式访问终端窗口,则可以动态创建Shell脚本并使用xterm运行该脚本。

Example: 例:

xterm -e path-to-script.sh

To keept the application open after execution use the -hold option. 要使应用程序在执行后保持打开状态,请使用-hold选项。

Minimalistic example: 简约示例:

echo "echo hello;/bin/bash" > /tmp/tmp123.sh; chmod +x /tmp/tmp123.sh;xterm -hold -e /tmp/tmp123.sh

For completeness, this is how it could be done on Windows (replace echo hello with the commands you want to execute): 为了完整起见,这是在Windows上可以完成的操作(用您要执行的命令替换echo hello ):

start cmd /C echo hello

To keep the window open after execution use the /K option instead of /C . 要使窗口在执行后保持打开状态,请使用/K选项而不是/C

Short answer: you can't. 简短的答案:您不能。

Long answer: the problem is, even if you successfully invoked your terminals, you'd need to know what the tty for each of these terminals in, which means you'd need to know what its stdin is... Provided that the OS doesn't do some strange mechanics which makes it so that the terminals don't even use file descriptors 0, 1 and 2 for stdin, stdout and stderr. 长答案:问题是,即使您成功调用了终端,您也需要知道其中每个终端的tty,这意味着您需要知道其标准输入是什么...只要操作系统并没有做任何奇怪的修改,因此终端甚至不使用stdin,stdout和stderr的文件描述符0、1和2。

And even if you could do that, you'd need to be able to write to the tty anyway. 而且即使您可以做到这一点,也仍然需要能够写tty。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM