简体   繁体   中英

How to get Jar File Path

I have an Verification.jar file which is checking some conditions in excel file. I want ot create a folder called Log at the same level where that Verification.jar is present.

Thanks in Advence.........

EDIT

I am Having Folders as follows

" Verification" :- Build,dist,lib,nbproject,src,Test,build.xml

"Build" Contains Classes folder

"dist" contains Verification.jar and Lib folder

"lib" Contains library files

I want to find the path of Verification.jar and create the Folder called Output .

If i change the drive it should create the folder at the same level where Verification.jar is present.

 String path= getClass().getResource("").getPath();

Output:--getPath:/C:/Java/jdk1.6/bin/Verification/lib/

Use URL Class.getResource(String name) method.

package.ClassName.class.getClassLoader().getResource("package.ClassName");

EDIT:

Have a look at great SO thread - How to get the path of a running jar file? suggested by @Hovercraft Full Of Eels

The correct form is:

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

If You are running a .jar file it returns the present working directory, this is the directory where the .jar is located...

Note that IDEs don't run the jar file, the run the .class file where the main method is located, so when running in an IDE this method returns the projects folder

PD: Sorry if I made grammar mistakes, English is not my native language.

Is not the best solution, but I use this function. The jar file must be named same as the project. Works on windows and linux. If some can help me inprove it I will be honered.

public String getJarCurrentJar() {
            String[] userDir = System.getProperty("user.dir").split("\\"+System.getProperty("file.separator"));
            String appName = "";
            for (String temp : userDir) {
                appName = temp; //Get the application name/jar
            }
            return System.getProperty("user.dir") + System.getProperty("file.separator") + "dist" + System.getProperty("file.separator") + appName+".jar";
        }

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