MRalwasser
MRalwasser

Reputation: 15973

How to retrieve useful system information in java?

Which system information are useful - especially when tracing an exception or other problems down - in a java application?

I am thinking about details about exceptions, java/os information, memory/object consumptions, io information, environment/enchodings etc.

Upvotes: 7

Views: 1702

Answers (5)

unmaskableinterrupt
unmaskableinterrupt

Reputation: 408

One thing that really helps me- to see where my classes are getting loaded from.

obj.getClass().getProtectionDomain().getCodeSource().getLocation();

note: protectiondomain can be null as can code source so do the needed null checks

Upvotes: 0

krock
krock

Reputation: 29619

Check out the Javadoc for System.getProperties() which documents the properties that are guaranteed to exist in every JVM.

Upvotes: 1

MRalwasser
MRalwasser

Reputation: 15973

In addition, for java servlet applications:

response.getCharacterEncoding()
request.getSession().getId()
request.getRemoteHost()
request.getHeader("User-Agent") 
pageContext.getServletConfig().getServletContext().getServerInfo()

Upvotes: 0

MRalwasser
MRalwasser

Reputation: 15973

For pure java applications:

System.getProperty("org.xml.sax.driver") 
System.getProperty("java.version")
System.getProperty("java.vm.version")
System.getProperty("os.name")
System.getProperty("os.version")
System.getProperty("os.arch")

Upvotes: 0

Sergii Pozharov
Sergii Pozharov

Reputation: 17848

Besides the obvious - the exception stack trace - the more info you can get is better. So you should get all the system properties as well as environment variables. Also if your application have some settings, get all their values. Of course you should put all this info into your log file, I used System.out her for simplicity:

System.out.println("----Java System Properties----");       
System.getProperties().list(System.out);

System.out.println("----System Environment Variables----");
Map<String, String> env = System.getenv();
Set<String> keys = env.keySet();
for (String key : keys) {
    System.out.println(key + "=" + env.get(key));
}

For most cases this will be "too much" information, but for most cases the stack trace will be enough. Once you will get a tough issue you will be happy that you have all that "extra" information

Upvotes: 2

Related Questions