How does java jdb eval multiple line code?

283 views Asked by At

like for-loop

is there any solution to evalute code dynamicly?

eval also unsupport import keyword.

main[1] eval java.lang.Class.forName("java.util.stream.IntStream").getDeclaredMethods()
 java.lang.Class.forName("java.util.stream.IntStream").getDeclaredMethods() = instance of java.lang.reflect.Method[51] (id=767)

main[1] print java.util.stream.IntStream.range(0, 3)
 java.util.stream.IntStream.range(0, 3) = "java.util.stream.IntPipeline$Head@67b64c45"

main[1] eval java.util.stream.IntStream.range(0, 3).forEachOrdered(n -> { System.out.println(n); })
com.sun.tools.example.debug.expr.ParseException: Name unknown: n
 java.util.stream.IntStream.range(0, 3).forEachOrdered(n -> { System.out.println(n); }) = 空值

Java traditional for-loop also unsupported.

main[1]
main[1] eval  for (int i = 1; i <= 10; i++) {
            System.out.println(i);
无法识别的命令: 'system.out.println(i);'。请尝试获得帮助...
main[1]         }com.sun.tools.example.debug.expr.ParseException: Encountered " "for" "for "" at line 1, column 3.
Was expecting one of:
   

1

There are 1 answers

2
張根源 On

in the jdb session, if you run help, then you might see something like this:

Initializing jdb ...
> help
** command list **
...

print <expr>              -- print value of expression
dump <expr>               -- print all object information
eval <expr>               -- evaluate expression (same as print)
...

and the description of eval says evaluate expression, so it's not evaluate statement which is what you really want, please correct me if i am wrong, thanks!