M
M
MaxLich2017-10-10 20:16:32
Java
MaxLich, 2017-10-10 20:16:32

Why does my program not work as it should when I launch it with a double click?

Hello. The program connects to a remote database. All settings are written in a separate properties file. From IDEA everything works. If I run the assembled project from the command line (I compile, of course, into a jar, and run it like this: java -jar <file name.jar>), everything works too (the data is pulled from the database).
If I launch the program by double-clicking on the jar-nick, then the data from the database is not pulled out. What could it be?
UPD1: I tried it from Windows - everything works. For some reason it doesn't work on Linux (ubuntu).
UPD2: I tried to write the path in the code like this:

private static final String PATH_TO_FILE_PROPERTIES = new File("").getAbsoluteFile() + File.separator + "settings_db_mysql_net.properties";

And in the code I wrote the value of this variable to an external file. As a result, when I launch the jar with a double click, this file turns out to be searched in my home folder (in ubuntu). If I launch into ideas and through the console, then everything is fine.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxLich, 2017-10-12
@MaxLich

In general, I solved this problem. The result is this method:

private static String getClassLocation(Class<?> c) {
        URL pathURL = c.getProtectionDomain().getCodeSource().getLocation();
        try {
            Path path = Paths.get(pathURL.toURI());
            String rootStr = path.getRoot().toString();
            return rootStr + path.subpath(0, path.getNameCount()-1).toString() + File.separator;
        } catch (URISyntaxException e) {
            e.printStackTrace();
            return "";
        }
    }

I submit the class in which this method lies as an input. The result is the absolute path to the folder containing the jar file. Well, then everything is simple: I attach the name of the property file to this path, feed this string as input to the load () method from the Properties class (of course, before that I create an object of this class), and then I get the Properties object with the necessary data. Works on both Linux (Ubuntu) and Windows (Windows 7 in my case).

M
Michael, 2017-10-11
@SnowBearRu

Try to get the property file based on getClass().getResource()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question