简体   繁体   English

如何在Linux的Java程序中打开包含GUI的.bat?

[英]How do I open a .bat containing a GUI in a java program in Linux?

everyone. 大家。 I'm quite new here so please be tolerant if I make any mistakes. 我在这里很新,所以如果我有任何错误,请多包容。

I have a .bat file containing a command line to open up a .jar file that contains a program that has a GUI in it. 我有一个.bat文件,其中包含一个命令行以打开一个.jar文件,该文件中包含一个带有GUI的程序。 The only line that's in the .bat file is: .bat文件中的唯一行是:

    java -jar "NewServer.jar" 

I've been trying to use Runtime() to get this to run, but most the instructions I find to open a .bat file in a java program are for Windows. 我一直在尝试使用Runtime()来使其运行,但是我发现在Java程序中打开.bat文件的大多数说明都是针对Windows的。 I'm currently using Fedora 12 (don't tell me to upgrade, I can't) if that makes a difference and programming using Eclipse. 我目前正在使用Fedora 12(不要告诉我升级,我不能)是否有所作为,并使用Eclipse进行编程。 I also found this ProcessBuilder thing, but I couldn't get it to work so unless you have very explicit directions on how to use it, please don't include it in your answer. 我也找到了ProcessBuilder的东西,但是我无法使其正常工作,因此除非您对如何使用它有非常明确的指导,否则请不要在回答中包括它。 I would much rather use Runtime. 我宁愿使用运行时。 It looked simpler. 看起来更简单。

Here's my code to test using Runtime in a java program. 这是我在Java程序中使用运行时进行测试的代码。 I'm hoping that if I can get this to work, I can get it to work in my real program. 我希望,如果我能使它正常工作,那么我可以将其在我的真实程序中工作。

import java.io.IOException;

public class testbat {

  public static void main(String[] args) {

    Process proc = null;
    try {
        proc = Runtime.getRuntime().exec("./ myServer.bat");
    } catch (IOException e) {

        e.printStackTrace();
    }

      System.out.println("Cool");


}

The last line is just there for me to see if the program actually ran in case the GUI doesn't open. 最后一行是我要查看的程序是否在GUI无法打开的情况下实际运行。 Also, I've already tried many combinations of things to include in the area after ".exec". 另外,我已经尝试过将“ .exec”之后的区域进行多种组合。 I've tried using a path like "~/user/workspace/ProjectServer/dist/myServer.bat" to no avail. 我尝试使用“〜/ user / workspace / ProjectServer / dist / myServer.bat”之类的路径无济于事。

I also already know that .bat files are for windows, but I'm able to execute it in linux, so I don't know if that makes a difference. 我也已经知道.bat文件是用于Windows的,但是我可以在linux中执行它,所以我不知道这是否有所不同。 I also tried using a .sh file the same way and it didn't work. 我也尝试以相同的方式使用.sh文件,但没有用。

Please bear in mind that I'm not that great at Java, but I had to use it for this particular program, so if your answers could be really descriptive that would be awesome. 请记住,我不太擅长Java,但是我必须将它用于特定程序,因此,如果您的回答可以真正地描述性的话,那就太好了。

Just take that line out of the bat file, and run it. 只需从bat文件中删除该行,然后运行它即可。 Yo're making it too hard. 太难了。

$ java -jar "NewServer.jar"

will work. 将工作。 The quotes aren't necessary, so 不需要引号,所以

$ java -jar NewServer.jar

will work as well. 也会工作。 If you want to have the equivalent of your bat file, create a file named, say, run_newserver containing that line. 如果要获得与bat文件等效的文件,请创建一个名为run_newserver的文件, run_newserver包含该行。 Change its mode to executable: 将其模式更改为可执行文件:

$ cat > run_newserver
java -jar NewServer.jar
^D
$ chmod a+x run_newserver
$ ./run_newserver

Ideally, since you shouldn't have scripts without comments, do this. 理想情况下,由于您不应有没有注释的脚本,因此请执行此操作。 In your favorite editor, create a file run_newserver containing 在您喜欢的编辑器中,创建一个文件run_newserver,其中包含

#!/usr/bin/env bash
java -jar NewServer.jar

and chmod that. chmod The line with #! #!#! -- often called a "shebang line" -- is UNIX magic that lets you say what interpreter you want. -通常称为“ shebang行” -是UNIX的魔术,它使您能够说出所需的解释器。 The program env in usr/bin finds your program and runs it (needed because different systems put bash in different directories.) usr/bin env程序会找到您的程序并运行它(需要此操作是因为不同的系统将bash放在不同的目录中。)

You could even put explanatory comments in the file too. 您甚至可以在文件中添加解释性注释。

I'm a little unclear why you want to use Runtime#exec to run it at all -- it seems you'll just need a shell script to start that program. 我有点不清楚为什么您要使用Runtime#exec来完全运行它-看来您只需要一个Shell脚本即可启动程序。

Why are you using Java to run a Batch file, that in turn runs a Java program? 为什么要使用Java运行批处理文件,而批处理文件又运行Java程序? Why have Batch in the loop at all? 为什么批处理完全处于循环中? Just put the jar in your classpath and call it directly. 只需将罐子放在您的类路径中,然后直接调用即可。

Batch (.bat) files are only for Windows environment. 批处理(.bat)文件仅适用于Windows环境。 So, Try using shell script 因此,尝试使用Shell脚本

 Process proc = Runtime.getRuntime().exec("myServer.sh");

Just open up terminal and do this 只需打开终端并执行此操作

vi /dir/to/exec/exec.sh

tap "i" and write this 点击“ i”并写下

#!/bin/sh
java -jar "NewServer.jar"

or if you want to run it in the background 或者如果您想在后台运行

#!/bin/sh
java -jar "NewServer.jar" & > /tmp/JavaServer.log

hit esc and type ":wq" and you have saved the file. 按esc并键入“:wq”,您已经保存了文件。

type this into the terminal 在终端输入

chmod +x /dir/to/exec/exec.sh

this give executable privileges and then you should run the file like 这赋予可执行文件特权,然后您应该像运行文件

sh /dir/to/exec/exec.sh

Process is only initialized by your first call. 仅通过您的第一个调用来初始化进程。 You need to run: 您需要运行:

proc.waitfor();

to get it to actually run your app. 使其真正运行您的应用程序。

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

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