Installation directory path

JAVA

Installation directory path

Postby Sveta » Tue, 31 Aug 2004 05:49:13 GMT

Hello everybody,

My application needs to keep reading files from the directory in which it
will be intsalled. How can I implement this in my code?

I don't think that something like System.getProperty("user.dir") is what I
am after, as the user may have a batch file and run the application from
wherever he/she likes. I don't want the directory the application is running
from currently, but the actual installation directory.  (By the way, this
should also be cross platform ).

Any ideas?

Thank you in advance,

Sveta



Re: Installation directory path

Postby Paul Lutus » Tue, 31 Aug 2004 07:37:49 GMT




public class AppDirLocation {
   
   static public void main(String[] args)
   {
      AppDirLocation app = new AppDirLocation();
      System.out.println(app.getClass().getProtectionDomain()
      .getCodeSource().getLocation());
   }
}

This can be done statically as well:

(class name).getProtectionDomain().getCodeSource().getLocation()

-- 
Paul Lutus
 http://www.**--****.com/ 


Re: Installation directory path

Postby Andrew Thompson » Tue, 31 Aug 2004 09:42:53 GMT




..right idea, wrong user directory, put the files in "user.home"..
< http://www.**--****.com/ #path>

HTH

-- 
Andrew Thompson
 http://www.**--****.com/ 
 http://www.**--****.com/ 
 http://www.**--****.com/ 

Re: Installation directory path

Postby Paul Lutus » Tue, 31 Aug 2004 10:53:47 GMT






The OP actually wants the directory the running application is located in. I
know this isn't always possibe, but see my other post for one answer.

-- 
Paul Lutus
 http://www.**--****.com/ 


Re: Installation directory path

Postby Sveta » Tue, 31 Aug 2004 16:49:05 GMT

Many thanks,


would (class name).getProtectionDomain().getCodeSource().getLocation() work
for jar as well?

Sveta



Re: Installation directory path

Postby Paul Lutus » Tue, 31 Aug 2004 17:01:39 GMT




Why don't you try it? Don't you have access to a Java compiler? Create the
example program, run it, satisfy yourself that it works, then jar it and
try again.

-- 
Paul Lutus
 http://www.**--****.com/ 


Re: Installation directory path

Postby Sveta » Tue, 31 Aug 2004 19:28:03 GMT

OK, will also do this later. In the meanwhile I tried:

System.out.println("Loaded MyInterface from:" +
MyInterface.class.getProtectionDomain().getCodeSource().getLocation());


and it keeps coming up with null pointer exception messages.

Also tried
this.getClass().etc
and also this.getClass().getResource("MyInterface.class") all come up with
problems : java.lang.NullPointerException

Sveta




Re: Installation directory path

Postby Andrew Thompson » Tue, 31 Aug 2004 21:18:26 GMT




If MyInterface.class is in package mypackage, 
you need to reference it as..
this.getClass().getResource("mypackage/MyInterface.class");

But.. whay are you not importing this class?

Why are you trying to load it by name?

-- 
Andrew Thompson
 http://www.**--****.com/ 
 http://www.**--****.com/ 
 http://www.**--****.com/ 

Re: Installation directory path

Postby Sveta » Tue, 31 Aug 2004 22:04:51 GMT

Paul/Andrew

thanks for the suggestions.

Actually, both these now work (haven't tried with jar yet):

ClassLoader.getSystemClassLoader().getSystemResource("MyInterface.class"));
and MyInterface.class.getProtectionDomain().getCodeSource().getLocation());


The probelm was possibly that the first time I run the application through
the GUI IDE which may have interferred?? (and yes the application was in a
package). Then I stopped using the IDE but in the meanwhile I managed to
insert a bug in the init() method !
(Sorry for the extra message due to my fault...)

The whole point of doing this was to be able to dynamically get the names of
the files from which info is read.
I have a version using an environmental variable but I preferred not to have
to set anything and make the program able to sort things out by itself.
And I had no clue how to do this (importing? not sure how).

Sveta








with



Re: Installation directory path

Postby Paul Lutus » Wed, 01 Sep 2004 00:55:25 GMT




In order to get assistance, you must post a short, complete, compilable
example of the problem code.

-- 
Paul Lutus
 http://www.**--****.com/ 


Similar Threads:

1.Referencing Java Installation Path from ASP

Our developers are calling a java application from an ASP/ASP.net page. 
Excerpt as follows:

----
Dim a_jvmargs(1)
a_jvmargs(0)= "-Djava.class.path=C:\Program 
Files\Java\j2re1.4.2_04\lib\rt.jar;" &_
"C:\jar1.jar;" &_
"C:\jar2.jar;" &_
"C:\jar3.jar;" &_
"C:\jar4.jar"

set objJVM = Server.CreateObject("comJava.comJavaJvm")

objJVM.setVMArguments(a_jvmargs)
objJVM.setJavaVMLibraryPath("C:\Program 
Files\Java\j2re1.4.2_04\bin\client\jvm.dll")
objJVM.CreateJavaVM()
----

Note the hard coded references to the java installation directory. I.E. 
(C:\program files\Java\jre1.4.2_04)

Unfortunately hard coding the path to java isn't the most ideal solution for 
various reasons. Primarily upgrading java on our servers without breaking 
our app is problematic. Likewise it requires deploying old versions of java 
to new servers to retain compatibility. (For refernce purposes this code is 
being used by hundreds of websites across dozens of servers)

So my question is - is there any way to dynamically reference the latest 
java installation directory from ASP vs hard coding it?

Thanks
Brad 


2.How do we find installation directory for a server developed in java

Hi

I'm developing a server in Java. I need to read/write certain files in
my server code, for which I need the exact location of files.

In C++, normally this is achieved by having a system variable like
SERVER_HOME and using this variable in the code. This variable can be
set by Installer.

What is the way for achieving this in Java. 

Also my server is actually a web-app which will run in some app
server.

thanks,
Naresh

3.get the path to the directory of the current user

Hi,

I work with Netscape7 and Mozilla. Is there anyone who knows
how to obtain the path to the directory containing infos of the current
profile? For information, I need this path to access the DB containing the
certifcates.
for example the path on my computer is :
"C:\Documents and Settings\ButayeR.BEBRU-BUTAYER\Application
Data\Mozilla\Profiles\default\6r49g0wk.slt"

If I'll have the path till "default" it would be fun.

Thanks by advance,

Rodrigue Butaye


4.How to get path to current directory?

Hello.

How to get path to current directory and put this path to object of class
String?
I found FileSystemView.getHomeDirectory() but it returns path to home
directory for example:

C:\Documments and Settings\Thomas

Thanks for help.

5.Specify a directory in the Class-Path entry of the Manifest

6. get the path of current Java file directory

7. How to get a path to current directory?

8. GIJ and paths and packages and the current directory



Return to JAVA

 

Who is online

Users browsing this forum: No registered users and 57 guest