2

Main method in Java have the String[] as input parameter to the main method, but i want to pass object as a parameter. Java and all command line parametrized languages accept only String array of arguments which make sense as our ability to call using command line gives us the ability to enter text that could be represented by String object in Java

This is how we used to have a method like:

public static void main (String[] args) {

for (String text: args) {
System.out.println(text);
}
}

If we need to pass integer value, we need to use parsing method to obtain the numeric value from it.

 try {
 Arg = Integer.parseInt(args[0]);
 } catch (NumberFormatException e) {
 System.err.println("Number format mismatch");
 }

What if we need to pass a whole object as an argument to the class?

Regards

Girish K
  • 758
  • 1
  • 13
  • 24

5 Answers5

2

No. You can't change it. This is because the command line is only really a block of text. It makes no sense for a command line to try figure out what data type the text is.

That would mean the command line would need to have prior knowledge about the data types available in whatever is about to be run and somehow create (in the case of Java) the correct Objects and cast to them.

Ross Drew
  • 8,163
  • 2
  • 41
  • 53
2

There is no way to pass anything but string, because in command line you in fact can type only strings.

Scadge
  • 9,380
  • 3
  • 30
  • 39
2

To my knowledge, you can only pass to the main knowledge in String format. This is because things passed to the main method come from System.in, either through random user input or things like piping, whereby you pass Strings from one Java program to another.

That being said, what you could do is that you create a method in your object class to parse a String form of that object to recreate the original version of that object.

For example:

public class myRectangle
{
     private int length;
     private int width;

     public myRectangle(int inLength, int inWidth)
     {
         this.length = inLength;
         this.width = inWidth;
     }

     // the rest of your class
     public String toString()
     {
          return "[" + length + ", " + width + "]";
     }

     public static Rectangle parseString(String input)
     {
          int firstBracketIndex;
          int commaIndex;
          int lastBracketIndex;

          firstBracketIndex = 0;
          commaIndex = input.indexOf(",");
          lastBracketIndex = input.length() - 1;

          String aWidth = input.substring(firstBracketIndex, (commaIndex - 1));
          String aLength = input.substring((commaIndex + 2), lastBracketIndex);

          return new Rectangle(Integer.parseInt(aWidth), Integer.parseInt(aLength));
      }

 }

Something like this could solve your problem. (There may be some off by one errors in my code and I wrote it out really long so it would be clear, but you get the idea!)

The point is, that you create a parsing method that is the inverse of your toString method, so that you can take copies of your class off the command line.

2

This is not possible. However, as a workaround you can move the logic from main to a separate function accepting the object or whatever you need. Then main would only parse its string argument and call your new function, which also could be called directly.

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
2

You can only pass a String into main. However nothing stops that String being a chunk of XML/JSon/whatever that you then parse into an object.

For example in JAXB:

 // Needs xml annotations etc
 class T {
 }

 public static void main(String[] args) {
       T t=null;
            JAXBContext jc = JAXBContext.newInstance(T.class); 
            try ( StringReader sr = new StringReader(args[0])) {
                t = (T)jc.createUnmarshaller().unmarshal(sr);   
            } catch (JAXBException ex) {
                // handle failure
            }
  }
Tim B
  • 40,716
  • 16
  • 83
  • 128