86339

File.getAbsolutePath incorrect on Ubuntu

Question:

This is a follow-up question to <a href="https://stackoverflow.com/q/13449594/475845" rel="nofollow">this one</a> (just as a brief description: I have been able to run a Java program by double-clicking on the .jar file on OS X and Windows, but not on Linux, as with the latter I get a file path problem).

Through trying out a few things using NetBeans under Ubuntu (12.04) I found that the problem seems to be located in what the program considers to be its working directory (which I concluded from the output of File.getAbsolutePath()). If I start my application in NetBeans, everything works (even under Ubuntu), and

System.out.println(new File(".").getAbsolutePath());

gives me /home/my_home/projects/VocabTrainer/., which is my project folder and thus correct. However, if I double-click on the .jar file, located in /home/my_home/projects/VocabTrainer/dist, the output I get under Ubuntu suddenly is merely /home/my_home/. This is problematic as I want to access a data file which is located in a sub-directory of my dist dir.

Does anyone know the cause of this behaviour, and how I can solve the problem?

PS: I don't know if this is required, but here's the output of java -version

java version "1.6.0_24" OpenJDK Runtime Environment (IcedTea6 1.11.5) (6b24-1.11.5-0ubuntu1~12.04.1) OpenJDK Server VM (build 20.0-b12, mixed mode)

Answer1:

The cause, not really, at the moment. But you may not want to handle it that way, due to the evident unpredictability. Something like this should get the file, assuming you use the qualified class name of something in the jar in the below getResource call.:

URL url = this.getClass().getClassLoader().getResource("thepackage/ofyourclass/JunkTest.class"); //get url of class file. expected: ("jar:file:/somepath/dist/yourjar.jar!qualified/class/name.class") File distDir = null; if(url.getProtocol() == "jar") { String classPath = null; String jarPath = url.getPath(); if(jarPath.matches(".*:.*")) jarPath = new URL(jarPath).getPath(); classPath = jarPath.split("!")[0]; distDir = new File(classPath).getParentFile(); //may need to replace / with \ on windows? } else { //"file" or none distDir = new File(url.toURI()).getParentFile(); } //... do what you need to do with distDir to tack on your subdirectory and file name

EDIT: I should point out this is clearly hacky. You may be able to add the file's location to the classpath directly at startup (or include the file you're looking for in the jar). From this you could use this.getClass().getClassLoader().getResource() with the filename of what you're looking for directly, which would get you to something like:

URL url = this.getClass().getResource("yourfile"); File file = new File(url.toURI()); //... use file directly from here

FURTHER EDIT: ok, adapted to your missing protocol, and spread it out, so error messages will be more readable for you.

Answer2:

I think you're confusing the location of the JAR with the <a href="http://en.wikipedia.org/wiki/Working_directory" rel="nofollow">current working directory</a>.

To determine the former, see <a href="https://stackoverflow.com/questions/320542/how-to-get-the-path-of-a-running-jar-file" rel="nofollow">How to get the path of a running JAR file?</a>

Recommend

  • Spark task duration difference
  • Any real use for table-per-concrete-class with implicit polymorphism?
  • Extracting infromation from multiple JSON files to single CSV file in python
  • Why does Internet Explorer sometimes trigger a local storage event before the data's ready? [du
  • Could be Text orientation of JTextArea changed by keyboard shortcut?
  • How to create MSBuild targets file to deploy files?
  • Xcode duplicated devices bug
  • Objective C - Create a framework for my iphone apps?
  • Python 2.7 on OS X: TypeError: 'frozenset' object is not callable on each command
  • Install different versions of nuget packages inside one solution file with two projects
  • How to format code on aptana 3?
  • Hibernate in Glassfish - Ejb3Configuration NoClassDefFoundError
  • Using Netbeans with Cygwin and SDL, including SDL.h creates strange error
  • Cursor in wrong place in contenteditable
  • Create function that can pass a parameter without making a new component
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Alamofire and Reachability.swift not working on xCode8-beta5
  • Python 3.2.2, error(scripts to exe)
  • How can we prepend rows to a react native list-view?
  • Spring integration inbound-gateway Fire an event when queue is empty
  • Does Apportable support to build library binary (.a/.so)?
  • Cloud Code function running twice
  • List images(01.png) and descriptions(01.txt) from directory
  • Copy to all folders batch file?
  • error importing numpy
  • Do I need to reset a Perl hash index?
  • How to test if a URL from an Eclipse bundle is a directory?
  • Debugging ASP.NET on a built-in web server suddenly stops
  • VS2008 Enable C++ Exception with SEH
  • ORA-29908: missing primary invocation for ancillary operator
  • Why winpcap requires both .lib and .dll to run?
  • Apache 2.4 - remove | delete | uninstall
  • Calling of Constructors in a Java
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app