gurdy
gurdy

Reputation: 71

how to detect browser and OS in Java applet

I'm working on a complex Java applet which runs well in Safari and in various browsers on Windows and Linux but has problems on Macintosh in Chrome and Firefox. For debugging, it would be really helpful if Java code could detect and report the browser, the browser version, the OS, and the OS version when there are errors.

What is the easiest and most reliable way for Java to detect that info?

This looks good, but requires Apache log4j, and I don't know if I can convince the team to run that: http://code.google.com/p/user-agent-utils/'

I guess Java could get the user-agent string via JavaScript, but I can't find any good example code that clearly shows how to do that.

Upvotes: 2

Views: 9262

Answers (2)

user14335364
user14335364

Reputation:

In case you use JavaScript, then use the below code to detect Browser and OS of clients.

<p id="demo5"></p>

<script>
document.getElementById("demo5").innerHTML = "<span>your Browser is: <br> </span> " + navigator.userAgent;
</script>

<p id="demo66"></p>

<script>
document.getElementById("demo66").innerHTML = "<span>your oscpu (Operating System | CPU) is: </span> " + navigator.oscpu;
</script>

Upvotes: 0

BalusC
BalusC

Reputation: 1109570

You can get the necessary OS information by System#getProperty() as follows:

String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
String osArch = System.getProperty("os.arch");
String javaRuntimeVersion = System.getProperty("java.runtime.version");

For others, check the javadoc of System#getProperties().

The user agent string is not so relevant. It's after all the same JVM and OS. Regardless, you could let the server side programming language get it from the current request headers and pass it as an <object> or <applet> parameter to the applet. Here's an example using JSP/EL:

<param name="userAgent" value="${header['user-agent']}" />

and obtain it in the applet as follows:

String userAgent = getParameter("userAgent");

Once having an user agent string at hands, you may find the webservice at http://user-agent-string.info more useful than the user agent utils at Google Code.

Upvotes: 3

Related Questions