• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Runtime behavior after command-line invocation

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Courtesy of education.oracle.com



Which command-line invocation will produce the output found?
a) java -Dx=y x y z
b) java -Px=y x y z
c) java -Dx=y x x y z
d) java -Px=y x x y z
e) java x x y z -Dx=y
f) java x x y z -Px=y

Option C is correct. -D sets a property and args[1] is the second argument (whose value is y)



This is what I understand:

1. The program is assigning the system property of "x" to String
2. When the runtime environment is invoked with c) java -Dx=y x x y z , it is setting the system property of "x" to "y". Then the program x is invoked. Next, args[0] get the value of "x". Then, args[1] gets the value of "y". This value of "y" is equal to the system value of "x", which is "y". Hence, "found" is printed.

Here is my question

When I invoke the runtime environment with

java x x y z -Dx=y

or

java x x y z -Px=y

Why do I get the following runtime error:

Exception in thread "main" java.lang.NullPointerException
at x.main(x.java:4)



We are invoking the runtime environment with keyword "java", the name of the program "x", and the arguments that follow.

Please guide.
 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From what I see here

java [ options ] class [ argument ... ]
java [ options ] -jar file.jar [ argument ... ]
javaw [ options ] class [ argument ... ]
javaw [ options ] -jar file.jar [ argument ... ]

options



the command-line-options always come before the filename and the args.

So in the two cases that you mentioned, the -Dx=y or -Px=y wouldn't be treated as command-line-options but rather as command line arguments.

That's what I think. You can test it by printing out all the command line args and checking if they get printed
HTH,
Vishwa
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishwanath Krishnamurthi wrote:

So in the two cases that you mentioned, the -Dx=y or -Px=y wouldn't be treated as command-line-options but rather as command line arguments.



But if they are treated like command-line arguments, why would the jvm throw an error. That's the disconnect.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since there is no property with the name x, so String p will be null. Then calling equals method on the null reference will result in a NullPointerException...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic