Solved

create a java application for RedHat and run the DMIDecode to return the data

Posted on 2011-03-13
3
1,514 Views
Last Modified: 2013-12-14
public class SerNum {

Could someone tell me what is wrong with the code below? I'm trying to create a java application that will run in RedHat. I would like to get the serial number of the system by using DMIDecode and return it as a string.


   


 public static String GetSerNum() {

        String[] command = { "dmidecode -s system-serial-number" };

        String sNum = null;
 

        try
         {

       Process SerNumProcess = Runtime.getRuntime().exec(command);

      BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));

            sNum = sNumReader.readLine().trim();

            SerNumProcess.waitFor();

            sNumReader.close();

        }

        catch (Exception ex)
        {
           

            sNum = "Did not work";

        }

        finally
        {

            return sNum;

        }

    }
0
Comment
Question by:bojeff30
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 35125442
whats the current problem with it?
0
 
LVL 4

Expert Comment

by:subratabiswas
ID: 35125685
Following is the same code that you provided, just did some formatting, added the necessary imports and the main function, and (importantly) declared the variable 'command' as a string.

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class SerNum {
    public static String GetSerNum() {
        String command = "dmidecode -s system-serial-number";
        String sNum = null;
        try {  
            Process SerNumProcess = Runtime.getRuntime().exec(command);
            BufferedReader sNumReader = new BufferedReader(new InputStreamReader(SerNumProcess.getInputStream()));
            sNum = sNumReader.readLine().trim();
            SerNumProcess.waitFor();
            sNumReader.close();
        } catch (Exception ex) {
            sNum = "Did not work";
        } finally {
            return sNum;
        }      
    }
    public static void main(String argv[]) {
        SerNum s = new SerNum();
        System.out.println(s.GetSerNum());
    }
}



See below the feedback from compilation and execution:

$ javac SerNum.java
$ export CLASSPATH=.
$ java SerNum
Did not work
$ dmidecode -s system-serial-number
/dev/mem: Permission denied

Same steps executed as root


# cd /home/subrata/temp
# export CLASSPATH=.
# java SerNum
5Y2BKLMZM1GM
# dmidecode -s system-serial-number
5Y2BKLMZM1GM

As seen in the above, there are many possible issues:

1) imports were missing; this would result into compilation error
2) main function was missing, this would also result in /missing main function' complaint
3) The original did not have execute privilege on the command dmidecode
4) Declared command as a simple string, not as an array of strings. The way it was provided, the whole string, including the intended arguments, were being considered as the executable, whlie naturfally there is no executable by that long name.

0
 
LVL 4

Accepted Solution

by:
subratabiswas earned 500 total points
ID: 35125864
To elaborate on 3) above, the original user that created the java program, compiled it and tried to execute it, did not have the necessary privilege to execute the underlying command dmidecode. On my test machine, this was a privileged command, so the command threw error message for non-privileged users. However, when the command was executed as root, it executed correctly, both when invoked from command prompt and indirectly from within  the java program.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question