简体   繁体   中英

Running .jar File on Linux

I have a .jar file that reads two files from within its current folder and produces as output a .txt file and a separate folder with multiple other .txt files. This works perfectly in Windows using this code to create the directory:

static String dir = System.getProperty("user.dir");

I used the instructions here: https://askubuntu.com/questions/192914/how-run-a-jar-file-with-a-double-click to set up my .jar file to run on a simple double-click, but as of right now, it does nothing when double-clicked. My guess is that the above line of code does not translate well to Linux. Anybody know how to resolve this?

First, try running it on the command-line, with

java -jar <file.jar>

The user.dir property is cross-platform (see here ) so it should not be the problem. However, are you using correct file separators? Remember it's '/' on UNIX and '\\' on Windows.

尝试java -jar Jarname.jar并在此命令后将其他文件作为参数传递

The code line you gave works fine on linux.

My best guess is that you're then trying to use this directory path by adding a windows-specific path separator (like path + "\\subdir") which isn't appropriate for linux (you should build a new File object instead).

Either that, or your jar file isn't being executed at all. Have you tried doing something very simple in your jar file to see if anything is being run? Have you tried running your jar with java -jar myapp.jar to see if any exceptions are thrown or error messages displayed?

You will need to manually tweak your build process to get the jar file marked as executable. In your build xml file, there is a target, "-post-jar", that is called after the jar is built. You'll need to make that target and use Ant's chmod task to modify your jar. Once you do that it will occur every time you make a jar file in that project.

It will run fine as long as you have a JRE installed.

Read this article to know more .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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