无法访问jarfile? [Ubuntu,Linux]

时间:2015-08-12 07:41:12

标签: java linux ubuntu

我一直试图解决这个问题几天,但我似乎无法理解为什么这不能正常工作。我试图从ProcessBuilder启动一个jar文件,但它只是说没有找到jar文件。如果我从终端运行完全相同的命令,那么它就像魅力一样运行。任何帮助将不胜感激。

注意:参数s是服务器文件,我已经确认正在传递正确的参数。

以下是该方法的缩写版本(它在pb.start上出错):

public static void startServer(String s){
    File server = new File(DataManager.getWorkingDirectory() + "/Servers/" + s + "/");
    if(server.exists()){
        if(serverProcess == null){
            try{
                String OS = System.getProperty("os.name").toLowerCase();
                boolean isLinux = (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0) ? true : false;
                //java -jar -XX:MaxPermSize=126M -Xmx1G -jar ...(PathToJar)/server.jar
                ProcessBuilder pb = new ProcessBuilder("java", "-jar", "-XX:MaxPermSize=" + permSize + "M", "-Xmx" + ramAmount + "G", "\"" + (!isLinux ? DataManager.getWorkingDirectory().replace("\\", "/") : "") + "Servers/" + s + "/server.jar\"", "nogui");
                if(!isLinux){
                    pb.directory(server);
                }else{
                    pb.directory(new File(DataManager.getWorkingDirectory()));
                }
                logger.info("Starting Server...");
                final Process p = pb.start();
                GUIMain.serverProcess  = p;

以下是整个方法:

public static void startServer(String s){
    File server = new File(DataManager.getWorkingDirectory() + "/Servers/" + s + "/");
    if(server.exists()){
        if(serverProcess == null){
            try{
                String OS = System.getProperty("os.name").toLowerCase();
                boolean isLinux = (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0) ? true : false;
                //java -jar -XX:MaxPermSize=126M -Xmx1G -jar ...(PathToJar)/server.jar
                ProcessBuilder pb = new ProcessBuilder("java", "-jar", "-XX:MaxPermSize=" + permSize + "M", "-Xmx" + ramAmount + "G", "\"" + (!isLinux ? DataManager.getWorkingDirectory().replace("\\", "/") : "") + "Servers/" + s + "/server.jar\"", "nogui");
                if(!isLinux){
                    pb.directory(server);
                }else{
                    pb.directory(new File(DataManager.getWorkingDirectory()));
                }
                logger.info("Starting Server...");
                final Process p = pb.start();
                GUIMain.serverProcess  = p;
                Thread thread = new Thread(){
                    public void run(){
                        try{
                            if(serverList != null){
                                serverList.setEnabled(false);
                            }
                            BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                            BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
                            String s = null;
                            while ((s = stdInput.readLine()) != null) {
                                if(s.toUpperCase().contains("EULA")){
                                    updateTextPane("<font color=\"red\">" + s.replace("Go to eula.txt for more info.", "Please press the <font color=\"green\">Agree to EULA</font> button to agree to the EULA.") + "</font>");
                                    runCommand("stop");
                                }else{
                                    updateTextPane(s);
                                }
                            }
                            while ((s = stdError.readLine()) != null) {
                                updateTextPane(s);
                            }
                            try{
                                Field field = p.getClass().getDeclaredField("hasExited");
                                field.setAccessible(true);
                                while (!(boolean) field.get(p)) {}
                            }catch(Exception e){
                                CrashReport.logStackTrace(e.getMessage(), e.getStackTrace());
                                updateTextPane("<font color=\"red\">Server is offline.</font>");
                                GUIMain.serverProcess = null;
                                if(serverList != null){
                                    serverList.setEnabled(true);
                                }
                            }
                        }catch(Exception e){
                            CrashReport.logStackTrace(e.getMessage(), e.getStackTrace());
                        }
                    }
                };
                thread.start();
            }catch(Exception e1){
                CrashReport.logStackTrace(e1.getMessage(), e1.getStackTrace());
            }
        }else{
            updateTextPane("<font color=\"red\">Server is already running!</font>");
        }
    }else{
        logger.error("The server " + server.toString() + " doesn't exist!");
    }
}

2 个答案:

答案 0 :(得分:0)

最有可能发生这种情况是因为您在将路径传递给ProcessBuilder时引用路径:

"\"" + (!isLinux ? DataManager.getWorkingDirectory().replace("\\", "/") : "") + "Servers/" + s + "/server.jar\""

ProcessBuilder负责引用参数,因此您最终会使用双引号。请尝试以下方法:

(!isLinux ? DataManager.getWorkingDirectory().replace("\\", "/") : "") + "Servers/" + s + "/server.jar"

另一件看起来很可疑的事情是在Linux下使用相对路径。这也可能是一个问题。

答案 1 :(得分:0)

我认为您需要拥有如下命令

.Value