Getting absolute path of a JAR from its classfiles?

I am creating a class witch is going into a jar-file. With this jar-file I will also have another file (config/data-file). That file will allways be in the same directory as the jar-file.
What I am wondering is: How can i programatically get the path of the "current" JAR file the "current" class is contained within, so that I can open/read the config/data file?
Today I use a method which i hope I can replace with a better method:
private static String jarfile="progconf.jar";
private static String projectname="ProgramConfig";
public static String getJarPath() throws Exception{
     try{
          String jvc = System.getProperty("java.class.path")+";";
          if(jvc.indexOf("\\"+jarfile+";")>0){
               jvc = jvc.substring(0,jvc.indexOf("\\"+jarfile+";"))+"\\";
               jvc = jvc.substring(jvc.lastIndexOf(":")-1); //Filsti
          }else if(jvc.indexOf("\\"+projectname+";")>0){
               jvc = jvc.substring(0,jvc.indexOf("\\"+projectname+";"))+"\\";
               jvc = jvc.substring(jvc.lastIndexOf(":")-1); //Filsti
          }else{
               throw new Exception("Klarte ikke finne JarPath!");
          return jvc;
     }catch(Exception ex){
          throw new Exception("getJarPath-" + ex.getMessage());
}-Wolflad

check out my replies at the link shown below:
http://forum.java.sun.com/thread.jsp?forum=57&thread=494736
;o)
V.V.

Similar Messages

  • How to get the absolute path of a file from the local disk given the file n

    how to get the absolute path of a file from the local disk given the file name

    // will look for the file at the current working directory
    // this is wherever you start the Java application (cound be C: and your
    // application is located in C:/myapp, but the working dir is C:/
    File file = new File("README.txt"); 
    if (file != null && file.exists())
        String absolutePath = file.getAbsolutePath();

  • How to get absolute path of a form within the Forms

    Aslam o Alikum (Hi)
    How to get absolute path of a form within the Forms 6i or 9i
    For example
    i am running a from "abc.fmx" from C:\myfolder directory
    can i get the form path 'C:\myfolder' by calling any any function from "abc.fmb"

    There is no direct call that will always work. What you need to do is call get_application_property(current_form). This may have the full path in it, depending on if that path was defined when the form was launched. If there is no path, then you need to use TOOL_ENV.GETVAR to read the Forms<nn>PATH and the ORACLEPATH, parse those out into individual directories and then check for the FMX in each.
    I already have some code to do all this for you see:
    http://www.groundside.com/blog/content/DuncanMills/Oracle+Forms/?permalink=4A389E73AE26506826E9BED9155D2097.txt

  • Path to rt.jar from C program

    How do I get path to rt.jar from a program written in C.
    Thank you

    Just like anything else you do in C. Pass in in via the command line or use a config file or use an environment variable or parse the registry (OS specific), etc.

  • Getting absolute path of a file running in remote machine

    Hi,
    I want to get the absolute path of a file that will be created in the remote server. Inside a directory XYZ in remote machine, the file will be created and the remote machine's address is xxx.xx.x.x. I have used the following snippet
    File f=new File(Filename);
    f.getAbsolutePath();
    Now, in the output am getting the absolute path of the file as d:/XYZ/Filename. Is there any way to get the output as xxx.xx.x.x/XYZ/FileName ? Can someone please suggest why the server details are not fetched and instead the directory name is fetched.
    I am running the program to get absolute path of a file in my remote machine. and for accessing that remote file from my machine, I use xxx.xx.x.x/XYZ/FileName.
    Thanks in advance.

    I'm going to take it a step further and guess at what the OP is going to say next.
    On host w.x.y.z there's a file /a/b/share/some_dir/file.txt.
    Host w.x.y.z is sharing the /a/b/share/ directory as SHARE.
    Client mounts \\w.x.y.z\SHARE as D:, so that he sees D:/some_dir/file.txt.
    OP now wants to know not just that D:/some_dir/file.txt is \\w.x.y.z\SHARE/some_dir/file.txt, but also wants to know that on w.x.y.z, SHARE corresponds to /a/b/share.
    @OP: If that's what you want, forget it. Part of the premise of file common drive/directory sharing protocols is that the client does not know or care and does not need to know or care what the absolute path is on the serving host. It's none of his business.

  • How to get absolute path of current directory?

    Hi, everyone!
    I want to get the absolute path of current directory.
    Are there some APIs that I can use?
    Thanks in advance,
    George

    Hi, dheeraj_tak buddie!
    What do you mean in your reply?
    "in File class u have all this methods for getting absolute path "regards,
    George

  • Probelm in getting absolute path of the application folder?

    I have problem in getting the absolute path of the application folder.
    I wriiten the following line in my action class:
    String absPath = getServlet().getServletContext().getRealPath("/");
    when i run my application 'DTH' in Tomcat server, I am getting correct path :
    C:\Tomcat\jakarta-tomcat-5.0.25\webapps\DTH
    But when i run this application in weblogic server, I am getting null value for the above line.
    Can any one help me in this?

    public java.lang.String getRealPath(java.lang.String path)
    This method returns null if the servlet container cannot translate the virtual path to a real path for any reason (such as when the content is being made available from a .war archive).
    so, getRealPath() might not be a very good approach because of its different behavior when used from a war or exploded application.
    What you could do is:
    1. pass the path as a parameter inside web.xml (config files, log files etc)
    2. For dynamic lookup of files you could make use of ClassLoader.getResource().
    Hope it helps.

  • Get absolute path of a servlet/jsp

    <p>
    I have deployed an application on JBoss. I need to get the absolute path of a servlet. I used following statement to do that.
    getServletContext().getRealPath("");
    but from this i get the following
    C:\jboss-4.0.2\server\default\.\deploy\sdb.ear\sdb.war\employer
    i don't know why there is a dot in between the path which is not correct.
    My application.xml is as follow
    <module>
    <ejb>sdb.jar</ejb>
    </module>
    <module>
    <web>
         <web-uri>sdb.war</web-uri>
         <context-root>/</context-root>
    </web>
    </module>
    Pls help me with this

    kaviratne wrote:
    but from this i get the following
    C:\jboss-4.0.2\server\default\.\deploy\sdb.ear\sdb.war\employer
    i don't know why there is a dot in between the path which is not correct.Me neither, but that dot shouldn't harm. It just means "current directory". Effectively that path is exactly the same as:
    C:\jboss-4.0.2\server\default\deploy\sdb.ear\sdb.war\employerWhich problems are you occurring with this? It might be worth an issuereport at the JBoss site though.

  • Getting exe path - JSmoothed my jar

    I created an application and I read the path of my jar code like this:
    Class cls =myClass.class;
            ProtectionDomain domain = cls.getProtectionDomain();
            CodeSource source = domain.getCodeSource();
            URL url = source.getLocation();
            String path=url.toString();
            path=path.substring(0,path.lastIndexOf('/'));Let�s suppose my app.jar is located in "C:/Program and Files/mySoftware/bb.jar"
    Then, my path returns "C:/Program and Files/mySoftware"
    Now I wrapped my jar into .exe file using Jsmooth(yes, I need to do that)
    When I run my .exe file it finds a different code location like this:
    "C:/Program and Files/tmp/images".
    Is there a different way of grabbing the exe path?
    Thx!!

    ThomYork wrote:
    Hi again DrLaszlo.
    I tried all the previous answers on the thread about the path.
    Now that I created a file .exe for my application, I could get its path using
    String tmp = System.getProperty("user.dir");
    Actually that's how you get the current working directory. Probably you set that to be the directory where the exe was located, but you can't rely on that in general.
    Since I was trying to load an image file I had the MalformedURLException when I created
    the url using the path.
    So, I verify if "tmp" starts with "file:\". If not, I add it and works fine.
    if(!tmp.startsWith("file:\\")){
         tmp="file:\\"+tmp;     
    You should use slashes in URLs, not backslashes.

  • Getting absolute path of a file in a webapp without using servlet/JSP

    Hi all,
    I need a small clarification. Is it possible to reterive the absolute path of a file present in a tomcat web application without using Servlet/JSP. I have a normal java class which uses this file to read the configuration parameters. I like to know whether I need to create a seperate servlet which will read the parameters from web.xml.

    Hi all,
    I need a small clarification. Is it possible to
    reterive the absolute path of a file present in a
    tomcat web application without using Servlet/JSP. What if the file is in a WAR? What do you do then?
    I
    have a normal java class which uses this file to
    read the configuration parameters. There are other, better ways to do this.
    I like to know
    whether I need to create a seperate servlet which
    will read the parameters from web.xml.What do you really want it for? You shouldn't need an absolute path. Use the class loader to get an InputStream.
    %

  • Get absolut path of a file

    when I write to a file in the actuel directory,
    e.g fw = new FileWriter("file.txt");
    how I can get the absolute path of this file? (e.g. C:\dir\file.txt)
    thanks stn

    Captain Obvious says ..... File.getAbsolutePath();
    Thank you, Captain Obvious.

  • Get installation path of web application from class file

    Can anybody tell me how I can get the path on disk to the root of my web application from a class file. Im running under Tomcat 5.5 and I have 3 copies of the same webapp under 3 differenet directories:
    C:\xxx\dev\yyy
    C:\xxx\test\yyy
    C:\xxx\prod\yyy
    Based on which directory the web-app, I need to do a lookup on a properties file to pull the right jdbc connection to connect to the appropriate database. So it would be similar to using getServletContext().getRealPath("/") from a .jsp file.
    Thanks,
    Jason

    Sorry for not clarifying. I have a properties file with name/value pairs. I know where this properties file is and I can do a lookup in it. In this file, I have jdbc connection names which point to corresponding jdbc resources in my context.xml file.
    jdbcDevDatabase=jdbc/tfmDev
    jdbcTestDatabase=jdbc/tfmTest
    jdbcProdDatabase=jdbc/tfmProd
    These connections are for dev, test and prod databases. Now based on where the web-app is installed on disk, i.e-dev dir, test dir, prod dir, I want to retrieve the corresponding connection. The problem is I don't know how to get a handle to the web apps real path on disk without access to the request object or using getServletContext().getRealPath("/") which are not available to me in a class file.
    Thanks,
    Jason

  • FileReference / Getting Absolute Path of Upload File

    Hi all,
    I am relatively new to Flex and I am using Flex SDK 3.4.
    I am trying to upload a file to my DB using FileReference.
    However, I am unable to get the absolute path of my chosen file, to be displayed in my textbox.
    Requesting your help on this issue.
    Regards,
    Arvind Raj

    I use the following to find the directory src in the WEB-INF/classes directory        final URL root = getClass().getResource("/src");
            final File rootFile = new File(root.getFile());

  • Getting absolute path of webapps directory

    Hi,
    Using a Tomcat server, i would like to get the absolute path of the Tomcat/webapps/ directory in one of my JSP pages, without using functions related to Servlet context...is it possible?
    In fact my real problem is that i want to open an existing text file in my JSP page. To do so i need the absolute path of it. This file must also be stored in Tomcat/webapps.
    Can anybody help me? i've search the forum and found some others topics like this one but didn't find my answers.
    Thanks

    Hi,
    Using a Tomcat server, i would like to get the
    absolute path of the Tomcat/webapps/ directory in one
    of my JSP pages, without using functions related to
    Servlet context...is it possible?
    Any specific reason for not using ServletContext's getRealPath() method ?
    In fact my real problem is that i want to open an
    existing text file in my JSP page. To do so i need
    the absolute path of it. This file must also be
    stored in Tomcat/webapps.Hmm,, file i/o is evil. You could use the methods of Class and ClassLoader to read in the file as a stream. The only requirement is that they be in the classpath.
    More info here - http://www.javaworld.com/javaqa/2003-08/01-qa-0808-property_p.html
    cheers,
    ram.

  • Getting Absolute Path of Class orJSP

    I want to know the absolute path of JSP page and any java class in web project.
    file.getAbsolutePath() Not work as only provide the tomcat path.
    Any help!

    Check this out:
    [http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)|http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)]

Maybe you are looking for

  • Nginx with php-fpm, connection with php-fpm.sock failed

    I am feeling stumped, trying to setup NGINX for the first time on Arch. Followed the wiki a few times and keep getting hung up in the same spot. I followed the instructions to install NGINX in a chroot, then went on to setup php-fpm. I have not tried

  • Mid-2010 MacBook Pro Screen Blinks

    Hello, the evening before going abroad, my MBP screen began to blink on and off to a black screen before returning to a temporary normal state. I have tried a PRAM reset and noticed that the boot screen runs fine with no flicker, but both the login a

  • How can I use a weblogic server's root path in an XML?

    Hi all, I hope I posted this to the correct newsgroup... Anyway, I have a question about getting a Weblogic (9.2) server's root directory in an XML config file. I'm trying to use a log4j.xml file to set up a logger for my application, but when I use

  • What is the use of BPM in ARIS??

    hi, I am new to xi and would want to know the use of BPM in ARIS. Can anyone give me a scenario where BPM is used?? I need some documents to learn BPM.. thanking you in advance.

  • Unnessary disk access

    i just installed Arch Duke some hours ago and then i did "pacman -Syu". everything is running fine but sometimes, 1-3 times  a day, i see unnecessary disk-access. when you access the disk e.g copying the files from one place to the other then you can