Разве путь по умолчанию при создании и доступе к файлам в Java не должен быть с jar? - PullRequest
0 голосов
/ 20 февраля 2020

У меня проблема при создании или доступе к файлам из моей IDE (Netbeans), всплывающие новые файлы в папке проекта , но при запуске jar из терминала Linux, Linux Команда терминала mint, доступ к файлу находится в моей папке / home / user.

Следующий код показывает путь к моей папке "/ home / user".

showMessageDialog(null, new File("").getAbsolutePath());

Любая помощь будет принята с благодарностью.

1 Ответ

0 голосов
/ 23 февраля 2020

Я узнал, как проложить путь к банке. Может быть, это поможет кому-то еще.

Вот метод, который возвращает путь к папке фляги.

/**
 * Gets the path to the folder of the jar file
 * @return Returns the path to the jar's folder
 * @throws UnsupportedEncodingException 
 */
static String getJarPath() throws UnsupportedEncodingException {
    // Get path to the jar
    String path = Test.class.getProtectionDomain().getCodeSource().getLocation().getPath();
    // Decode the path whitespaces
    String decodedPath = URLDecoder.decode(path, "UTF-8");

    // Remove the jar from the end of the path //

    // Path variable that will be returned
    String newPath = File.separator;

    // Path split by file separator
    String[] pathSplit = decodedPath.split(File.separator);

    // Add each of the strings to the new path except the last one
    for (int i = 0; i < pathSplit.length-1; i++) {

        // Add a link of the path
        newPath += pathSplit[i] + File.separator;
    }

    // Return the path to the folder with the jar
    return newPath;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...