Solved

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

Posted on 2011-03-13
3
1,480 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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CENTOS DHCP Server / PXE/TFTP 14 155
ignore other .htaccess 2 58
lunix and unix command 21 86
Trying to install php56 on CentOS 7 get GPG error 5 29
I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

828 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