Java - Get absolute server path

How to get the absolute server location path on my machine?

Suppose I use a Glassfish server, then I need to get the absolute docroot location path from the sea fish, as shown below:

C:\glassfish3\glassfish\domains\domain1\docroot

At runtime, I need to create a file in this place using the java io package, for example:

C:\glassfish3\glassfish\domains\domain1\docroot\myfile.txt
+5
source share
6 answers

If you use GlassFish to run GlassFish, i.e. use asadmin start-domain|start-instance, then we offer the following hardware warranty:

The current JVM working directory is absolutely guaranteed to be a domain or server configuration directory. In the default case, it will be:

c:/glassfish3/glassfish/domains/domain1/config

If you want to write something in docroot by default, you can do this:

File f = new File("../docroot/yourfile");

, , java (, java -jar glassfish.jar), :

File f = new File(System.getProperty("com.sun.aas.instanceRoot") + "/docroot/yourfile");
+3

,

path = getClass().getProtectionDomain().getCodeSource().getLocation()

. WEB-INF/classes. - path.subString(0,path.indexOf("WEB-INF")).

, : Eclipse "" .

0

. WAR EAR , docroot . , , .

, , .

0

I don't know if this is the best, but it works for me :)

String path = new File("").getAbsolutePath() + File.separator + "docroot";
0
source

you can try the following:

System.getProperty("catalina.base");

And you can find other properties by looking at the next variable in debug mode.

Properties properties = System.getProperties();
0
source

To do this, you do not need to go the full path, because

when you create a file than the default, it will create on the ROOTserver

which is here C:\glassfish3\glassfish\.

Hope this helps you.

-1
source

All Articles