Hello everyone,
I made a research method that will allow me to retrieve the path that runs the class file. But I also want it to work for a jar.
Please help..
thank you
Hello everyone,
I made a research method that will allow me to retrieve the path that runs the class file. But I also want it to work for a jar.
Please help..
thank you
Do you need the path to the jar, or the currently executing path? either ways method will give you the execution directory, but it won't tell you where your executing jar is actually located.
If you are trying to load resources from your classpath and can't figure out the directory, stop where you are. Doing a new File(...) isn't going to get you what you want. Do a ClassLoader.getResourceAsStream(...), which will search your classpath for the file.
I have followed lot of forums and blogs to find the answer for the absolute path for Jar file and I find this, the following you have to consider for your solution and for the absolute path of java jar file your location may be slight different such as instead of D drive you might be having C drive and so on.
jar -cvmf user_interface_test.jar "D:\Java applications\classes"
I suppose we need to hardcode the directory paths to the jar file and the images directory as a classpath option. even though it seems non-dynamic to me (meaning that the user must know where these directories are and must type/modify these paths themselves.)The method file.getAbsolutePath() returns the absolute path of the given file.
I do not expect the user, when wanting to run the program, to type in "java -cp .;C:\path_to_jar_file;C:\path_to_jar_file\path_to_images_dir -Xmx256M -jar myprogram.jar" at the command prompt everytime. That's why I'd created a batch file to launch the program.
Therefore, in the final analysis, Java essence can only use the absolute path to look for resources. All looking for the relative path resources are nothing more.
Some convenient way. However, API are in the bottom to help us build the absolute path in order to find the resources!
Bookmarks