6 Replies - 8643 Views - Last Post: 28 December 2009 - 07:18 PM Rate Topic: -----

#1 delete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-December 09

How can I get the PC's serial number with Java?

Posted 28 December 2009 - 06:24 PM

This is not any homework assignment. I do some Java, a friend at work in the IT department knows this, and asked me if it were possible to get the serial # of a machine using Java. He's been using this code through VBScript to get that info. Can the same thing be done in Java? If not, can you write any method or class to launch the VBScript that CAN get the serial #, then save the dumped output, print to screen, etc? Can anyone point me to a place in the Java Docs that would have info on this, if possible?

On Error Resume Next
Dim System
if Wscript.Arguments.Count >0 then
sSystem=Wscript.Arguments(0)
end if
ComputerName = InputBox("Enter the name of the computer you wish to query")
winmgmt1 = "winmgmts:{impersonationLevel=impersonate}!//"& ComputerName &""
Set SNSet = GetObject( winmgmt1 ).InstancesOf ("Win32_BIOS")
for each SN in SNSet
MsgBox "The serial number for the specified computer is: " & SN.SerialNumber
Next



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: How can I get the PC's serial number with Java?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How can I get the PC's serial number with Java?

Posted 28 December 2009 - 06:33 PM

To get us started, show us what you have tried. Whether it's homework or not we do require you to show some effort before we will provide any code (and unfortunately showing some code in a different language does not count as a good faith effort)
Was This Post Helpful? 0
  • +
  • -

#3 delete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-December 09

Re: How can I get the PC's serial number with Java?

Posted 28 December 2009 - 06:48 PM

I'm not even looking for code here fellas, I understand your policy. I just want to know if it's possible to get the serial # of a computer in Java, or if I have to call the vbs to get the information. Something makes me think that Java doesn't have direct access to hardware, or it didn't use to (but I think that may have changed, since you can get IP's and MAC addresses and host names and such) If it can be done, I'll figure out how. Not asking anyone to do it for me, again, just need to know if it's possible.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: How can I get the PC's serial number with Java?

Posted 28 December 2009 - 06:54 PM

After some research I dont think it's possible to do this with pure Java. I did find this which may be useful to you. Good luck :)
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How can I get the PC's serial number with Java?

Posted 28 December 2009 - 06:56 PM

WEll using the above script as a template SURE.

Generally Java is not good at this kind of thing because of its "platform independent" nature. But you can use a library called JACOB (Java COM Bridge) to access windows COM compoents... this means that anything you can do with VBScript you can do in Java...

...however JACOB is woefully documented. I would suggest that you start with some of my snippets on using JACOB. I made them while trying to figure out how to use the com bridge.

Windows Network usage statistics
WMI Service Query with JACOB
Java Windows Battery Life
List Windows Codecs
Google Earth COM API in Java <--- has links to JACOB and I think talks a little about how to set things up.
Was This Post Helpful? 1
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: How can I get the PC's serial number with Java?

Posted 28 December 2009 - 07:15 PM

I started from the WMI Service Query with JACOB snippet and went to MSDN to look at the Win32_BIOS class and came up with this example:
package jacob.wmi;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

/**
 * Quick little example of using JACOB (an open source Java-COM bridge) to access
 * the windows WMI interface.
 * <br>
 * To use this snippet you will need to download and install JACOB. This is relatively easy:
 * I just uncompressed the zip and put the jacob.jar and the dll into my classpath.
 * 
 * @author NickDMax (at) DreamInCode
 */

public class ComputerSerial {

	/**
	 * List the services currently running on the host computer.
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String host = "localhost"; //Technically you should be able to connect to other hosts, but it takes setup
		String connectStr = String.format("winmgmts:\\\\%s\\root\\CIMV2", host);
		String query = "SELECT * FROM Win32_BIOS"; 
		ActiveXComponent axWMI = new ActiveXComponent(connectStr); 
		//Execute the query
		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(query));
		
		//Our result is a collection, so we need to work though the.
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		while (enumVariant.hasMoreElements()) { 
			item = enumVariant.nextElement().toDispatch();
			//Dispatch.call returns a Variant which we can convert to a java form.
			String serialNumber = Dispatch.call(item,"SerialNumber").toString();
			String name = Dispatch.call(item, "Name").toString();
			String version = Dispatch.call(item, "Version").toString();
			System.out.printf("Serial Number: %s -- Name: %s -- Version: %s",serialNumber,name,version );
		} 
	}
}



However I am not sure it really works... the result I got was:

Serial Number: 1 -- Name: Default System BIOS -- Version: UNKNOWN

So I know the query worked... but I don't think it returned what you are looking for.

You should be able to emulate the above VBSCript, the method I used was just the WMI Query format I am used to using in PowerShell...
Was This Post Helpful? 0
  • +
  • -

#7 delete  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 28-December 09

Re: How can I get the PC's serial number with Java?

Posted 28 December 2009 - 07:18 PM

Thanks for the tips. That's kind of what I was thinking, that it would be difficult to do this solely with Java. I think the easier route to take would be to take the vbs, use Java to launch it (then the vbs will store the data to a text file), then read that text file back into the Java program for printing, storage, etc.

EDIT: Looking more closely at your code and output, it looks like the query is being done (as you said) since it's returning some values (for example, "1 --" after Serial Number, "Default" after name", etc), but I'm not sure where those values are coming from. I might try a similar query as a vbs and see if it returns similar data. That should at least determine if the source of the data is flawed, or if there's a problem with the query itself or the language being used.

This post has been edited by delete: 28 December 2009 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1