Skip to content

Instantly share code, notes, and snippets.

@vaibhaw
Last active August 29, 2015 14:11
Show Gist options
  • Select an option

  • Save vaibhaw/6b3e4263e61ef965f86c to your computer and use it in GitHub Desktop.

Select an option

Save vaibhaw/6b3e4263e61ef965f86c to your computer and use it in GitHub Desktop.
Get location of jar file which is being run
package com.vaibhaw.imagesimilarity
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
public class Searcher {
static String jarPath = ".";
public void getJarPath() throws IOException{
URL url1, url2, url3 = null;
File f1 = null;
File f2 = null;
File f3 = null;
String path1, path2, path3;
url1 = Searcher.class.getProtectionDomain().getCodeSource().getLocation();
url2 = Searcher.class.getResource(Searcher.class.getSimpleName() + ".class");
url3 = ClassLoader.getSystemClassLoader().getResource(".");
System.out.println("url1: "+url1.toString());
System.out.println("url2: "+url2.toString());
System.out.println("url3: "+url3.toString());
try{
f1 = new File(url1.toURI());
if(f1 != null){
path1 = f1.getCanonicalPath();
System.out.println("f1: "+path1);
}
}
catch(Exception e1){ System.out.println("error " + url1.toString()); }
try{ f2 = new File(url2.toURI()); }
catch(Exception e1){ System.out.println("error " + url2.toString()); }
try{ f3 = new File(url3.toURI()); }
catch(Exception e1){ System.out.println("error " + url3.toString()); }
if(f2 != null){
path2 = f2.getCanonicalPath();
System.out.println("f3: "+path2);
}
if(f3 != null){
path3 = f3.getCanonicalPath();
System.out.println("f3: "+path3);
}
path1 = URLDecoder.decode(url1.getPath(), "UTF-8");
path2 = URLDecoder.decode(url2.getPath(), "UTF-8");
path3 = URLDecoder.decode(url3.getPath(), "UTF-8");
System.out.println("path1: "+path3);
System.out.println("path2: "+path3);
System.out.println("path3: "+path3);
}
public static void main(String[] args) throws IOException{
Searcher searchAgent = new Searcher();
searchAgent.getJarPath();
}
}
/* Output in Eclipse for a maven project
url1: file:/home/vaibhaw/workspace/SimilarityDemoUL/target/classes/
url2: file:/home/vaibhaw/workspace/SimilarityDemoUL/target/classes/com/vaibhaw/imagesimilarity/Searcher.class
url3: jar:file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
error jar:file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
f1: /home/vaibhaw/workspace/SimilarityDemoUL/target/classes
f3: /home/vaibhaw/workspace/SimilarityDemoUL/target/classes/com/vaibhaw/imagesimilarity/Searcher.class
path1: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
path2: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
path3: file:/usr/lib/jvm/java-6-openjdk-common/jre/lib/ext/pulse-java.jar!/
*/
/* Output in terminal - for a maven project
url1: rsrc:./
url2: rsrc:com/vaibhaw/imagesimilarity/Searcher.class
url3: file:/var/www/vhosts/vaibhaw/vaibhaw-webapp/prod9/public/demo/
error rsrc:./
error rsrc:com/vaibhaw/imagesimilarity/Searcher.class
f3: /var/www/vhosts/vaibhaw/vaibhaw-webapp/prod9/public/demo
path1: /var/www/vhosts/vaibhaw/vaibhaw-webapp/prod9/public/demo/
path2: /var/www/vhosts/vaibhaw/vaibhaw-webapp/prod9/public/demo/
path3: /var/www/vhosts/vaibhaw/vaibhaw-webapp/prod9/public/demo/
*/
/* Output in Eclipse for a simple Java project
url1: file:/home/vaibhaw/workspace/TempExp/bin/
url2: file:/home/vaibhaw/workspace/TempExp/bin/io/github/vaibhawchandel/ClassJarPath.class
url3: file:/home/vaibhaw/workspace/TempExp/bin/
f1: /home/vaibhaw/workspace/TempExp/bin
f3: /home/vaibhaw/workspace/TempExp/bin/io/github/vaibhawchandel/ClassJarPath.class
f3: /home/vaibhaw/workspace/TempExp/bin
path1: /home/vaibhaw/workspace/TempExp/bin/
path2: /home/vaibhaw/workspace/TempExp/bin/
path3: /home/vaibhaw/workspace/TempExp/bin/
*/
// Still need to figure out the exact reasons the paths are printed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment