Linux - 通过shell脚本运行jar文件时的IO异常

时间:2017-01-02 16:59:44

标签: java linux shell

我正在使用Debain 8.0 - jessie(64位)。

当我尝试从其目录外部运行一个jar文件到shell脚本时,我收到IO异常。

目录:“/ home / rscedit”

文件:“data(directory)”“run.sh(file)”“Webserver.jar(file)”

当我尝试从“/ home / rscedit”以外的任何地方运行“run.sh”时,我在jar文件中面临IO异常。

但如果我尝试从“/ home / rscedit”运行“run.sh”,它运行得非常好。

我想在启动时运行我的shell脚本,所以我应该可以从“/ home / rscedit”外部运行我的shell脚本了吗?

Shell脚本

#!/bin/sh
java -jar -Xmx20480m /home/rscedit/Webserver.jar
read –n1

执行shell脚本时出现的错误

java.nio.file.NoSuchFileException: ./data/log/ipn.log.lck
        at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
        at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
        at sun.nio.fs.UnixFileSystemProvider.newFileChannel(UnixFileSystemProvider.java:177)
        at java.nio.channels.FileChannel.open(FileChannel.java:287)
        at java.nio.channels.FileChannel.open(FileChannel.java:335)
        at java.util.logging.FileHandler.openFiles(FileHandler.java:459)
        at java.util.logging.FileHandler.<init>(FileHandler.java:326)
        at org.displee.utilities.logging.LogFactory.loadFileLogger(LogFactory.java:44)
        at org.displee.utilities.logging.LogFactory.<clinit>(LogFactory.java:19)

LogFactory.java

package org.displee.utilities.logging;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.*;

public class LogFactory {

    private static final String FORMAT = "%1$td-%1$tm-%1$tY %1$tH:%1$tM:%1$tS %4$s %2$s - %5$s%6$s%n";

    private static final Map<String, Logger> MAP = new HashMap<>();

    static {
        try {
            loadFileLogger("ipn", "./data/log/ipn.log");
            loadFileLogger("error", "./data/log/error.log");

            Logger logger = Logger.getLogger("console");
            logger.setUseParentHandlers(false);
            ConsoleHandler ch = new ConsoleHandler();
            ch.setFormatter(new ConsoleFormatter());
            ch.setLevel(Level.ALL);
            logger.addHandler(ch);
            register(logger.getName(), logger);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void register(String name, Logger logger) {
        MAP.putIfAbsent(name, logger);
    }

    public static Logger get(String name) {
        return MAP.get(name);
    }

    private static Logger loadFileLogger(String name, String path) throws IOException {
        Logger logger = Logger.getLogger(name);
        FileHandler fh = new FileHandler(path, true);
        fh.setFormatter(new ConsoleFormatter());
        logger.addHandler(fh);
        logger.setUseParentHandlers(false);
        register(name, logger);
        return logger;
    }

    private static class ConsoleFormatter extends Formatter {
        @Override
        public synchronized String format(LogRecord record) {
            Date date = new Date();
            date.setTime(record.getMillis());
            String source;
            if (record.getSourceClassName() != null) {
                source = record.getSourceClassName();
                if (record.getSourceMethodName() != null) {
                    source += " " + record.getSourceMethodName();
                }
            } else {
                source = record.getLoggerName();
            }
            String message = formatMessage(record);
            String throwable = "";
            if (record.getThrown() != null) {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                pw.println();
                record.getThrown().printStackTrace(pw);
                pw.close();
                throwable = sw.toString();
            }
            return String.format(FORMAT,
                    date,
                    source,
                    record.getLoggerName(),
                    "LOG",
                    message,
                    throwable);
        }
    }

}

我用来执行shell脚本的命令

exec /home/rscedit/run.sh

编辑:我的数据地图不仅包含日志文件,还包含网站文件等其他内容。

1 个答案:

答案 0 :(得分:3)

您的java程序似乎对工作目录很敏感。最简单的解决方案我知道,改变这个

java -jar -Xmx20480m /home/rscedit/Webserver.jar

(cd /home/rscedit ; java -jar -Xmx20480m Webserver.jar)

,更改Java,这个(以及您拥有模式的所有地方)

loadFileLogger("ipn", "./data/log/ipn.log");
loadFileLogger("error", "./data/log/error.log");

之类的(使其相对于$HOME),

loadFileLogger("ipn", new File(System.getProperty("user.home"), 
        "data/log/ipn.log").getPath());
loadFileLogger("error", new File(System.getProperty("user.home"), 
        "data/log/error.log").getPath());