Sign in to follow this  
liquidAir

[java] Need some help with Java

Recommended Posts

I want to write an applet in Java that will detect if the Microsoft VM and/or the Sun VM are installed, and also if Java Web Start is available. After doing this, I need it to pass some results that can be accessed via javascript. Any help will be appreciated. Thanks.

Share this post


Link to post
Share on other sites
You are correct. I messed around with Java a while back. To run a java executable (.jar), a VM needs to be installed. Windows does not know how to run them alone.

Share this post


Link to post
Share on other sites
Quote:
Original post by ukdeveloper
I'm not a Java programmer, but am I not right in saying that the Java applet won't run at all if you don't have any VM?
Exactly; so if no result is returned from the applet, there is no VM installed. I don't know if a Java applet can tell which VM it's running on, though [sad]

Share this post


Link to post
Share on other sites
Oops... ok, hopefully, this can get moved by a moderator to the appropriate forum.

On the other hand, if no VM is installed, this is where javascript comes in. Since javascript doesn't receive any results from the applet, because it doesn't run, then we can easily display that no VM is installed.

Share this post


Link to post
Share on other sites
No probs, happens all the time [smile]

I sent them a quick message asking it to be moved to the Java forum, since you will probably get better help there. I wish I knew more about Java so I could help you myself [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by liquidAir
I want to write an applet in Java that will detect if the Microsoft VM and/or the Sun VM are installed, and also if Java Web Start is available.

After doing this, I need it to pass some results that can be accessed via javascript.

Any help will be appreciated.


Reading your posts and inferring your level of knowledge of java I strongly suggest you tell us what you're trying to do, rather than how you're wanting to do it - there are several orthogonal ways to achieve what you asked, but some will be completely useless depending upon what you're trying to do.

e.g.

1. install java?
a. Use browser-detection to find if the user is running MSIE.
b. If so, use MSIE's built-in stuff to find out if they have the java plugin, and webstart, and what versions of each
c. If not, run the ActiveX control that installs java webstart and runs your webstart app
d. If not MSIE, check if windows
e. If so, run the ActiveX control anyway - it won't downlaod if you already have java
f. If not windows, insert a link to Sun's undocumented auto-install-java page which they don't want to tell anyone about because their marketing dept are all morons

2. just guarantee your applet runs?
a. All versions of windows already have java.
b. ...but linux probably does not
c. ...and you can remove it from some web browsers
d. If someone's removed it, they probably don't want it (!), so don't you dare give it to them; have to display a "please install java, go here..."
e. If they don't have it in the first place, on linux you have little choice other than to detect the browser then provide the link to that browser's "install java plugin" website

3. ....run code optimize for differnet JVM's
a. Gets tricky, depending upon what you want to achieve...
redmilamber

Share this post


Link to post
Share on other sites
Alright, so after combining intensive Googling, trial and error, and bad coding practices *g*, I came up with this...

<html>
<head>
<title>Detect Java VM and Java Web Start</title>
</head>
<style type="text/css">
BODY { font-family: Arial, sans-serif }
TH { text-align: left }
</style>
<script language="javascript" type="text/javascript">
var browserInfo = navigator.userAgent;

// variable to check if Java Web Start is installed
var jwsInstalled = 0;

// Microsoft VM?
var msvmInstalled = 0;

// Sun VM?
var sunvmInstalled = 0;


// variable to check if client is MSIE
isIE = "false";

// If we are using
if(navigator.mimeTypes && navigator.mimeTypes.length)
{
var flag = navigator.mimeTypes['application/x-java-jnlp-file'];

if(flag)
{
jwsInstalled = 1;
}
}
else
{
isIE = "true";
}

// We are using another browser, not MSIE
// Opera, Mozilla, Firefox, etc...
if(isIE == "false")
{
javaEnabled = window.navigator.javaEnabled();
if(javaEnabled == "true")
{
javaVendor = java.lang.System.getProperty("java.vendor");

if(javaVendor.indexOf("Sun ") != -1 || javaVendor.IndexOf("sun ") != -1)
{
sunvmInstalled = 1;
}
}
}
// Our client is using MSIE
else
{
// check if ActiveX objects can be created
try
{
// Create Sun Java plugin ActiveX object
var pluginObject = new ActiveXObject("JavaPlugin");
// Create Java Web Start ActiveX object
var jwsObject = new ActiveXObject("JavaWebStart.isInstalled");
}
// they cannot be created
catch(e)
{
// Sun Java plugin and Java Web Start not installed
sunvmInstalled = 0;
jwsInstalled = 0;
}

// Sun Java VM object successfully created?
if(pluginObject)
{
// Yep! We have the Sun Java VM plugin installed
sunvmInstalled = 1;
}

// Java Web Start object successfully created?
if(jwsObject)
{
// Yep! We have Java Web Start installed
jwsInstalled = 1;
}

// Minimal Code Hack
// All versions of Windows up to 2000 have Microsoft VM installed by default
// No official Microsoft VM is distributed for Windows XP and above
// Users of Windows XP and above will be required to install the Sun Virtual Machine
var UserAgent = navigator.userAgent;

if(UserAgent.indexOf("Windows 95") != -1 || UserAgent.indexOf("Windows 98") != -1 || UserAgent.indexOf("Windows NT 5.0") != -1)
{
msvmInstalled = 1;
}
}
</script>

<body>
<table cellspacing="0" cellpadding="5" width="400">
<tr><th>Java Version</th> <th>Installed</th></tr>
<tr><td>Microsoft Java VM</td> <td><script language="javascript" type="text/javascript">if(msvmInstalled == 1) { document.write("yes"); } else { document.write("no"); }</script></td></tr>
<tr><td>Sun Java VM</td> <td><script language="javascript" type="text/javascript">if(sunvmInstalled == 1) { document.write("yes"); } else { document.write("no"); }</script></td></tr>
<tr><td>Java Web Start</td> <td><script language="javascript" type="text/javascript">if(jwsInstalled == 1) { document.write("yes"); } else { document.write("no"); }</script></td></tr>
</table>
</body>
</html>


[Edited by - liquidAir on May 30, 2005 1:31:13 PM]

Share this post


Link to post
Share on other sites
That seems to work well but I have an additional question. Is there a way to tell what VM will run if the web page encounters the <applet> tag instead of the <Object> tag?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this