Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
633 views
in Technique[技术] by (71.8m points)

groovy - MissingPropertyException in groovysh

I run groovysh, and type the following code:

groovy:000> String s = "Hello"
===> Hello
groovy:000> s
ERROR groovy.lang.MissingPropertyException:
No such property: s for class: groovysh_evaluate
        at groovysh_evaluate.run (groovysh_evaluate:2)
        ...
groovy:000>

How to access the s here?

(If I change String s = "Hello" to s = "Hello", I can access it. But I want to know how to access it in the example)


UPDATE

I want to use String s = "Hello" to define a variable because I want to declare the type of it. For example, if I write:

Date date = []

The date will be a java.util.Date. But if I write:

date = []

It will be a ArrayList.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The expression s = "Hello" sets a shell variable, the expression String s = "Hello" sets a local variable which does not get saved to the shell's environment. Please see the Groovy Shell documentation for more information. I am not quite sure what you are trying to achieve but you might rather want to go with Groovy Console to evaluate Groovy scripts.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...