Jshell - cannot find printf

Why was my jshell instance (JDK version 9-ea) unable to define the statement printf()? Below is the error that I am observing

jshell> printf("Print number one - %d",1)
|  Error:
|  cannot find symbol
|    symbol:   method printf(java.lang.String,int)
|  printf("Print number one - %d",1)
|  ^----^

I can access printf if I specify it in the usual way.

jshell> System.out.printf("Print number one - %d",1)
Print number one - 1$1 ==> java.io.PrintStream@1efbd816

Any pointers?

+4
source share
4 answers

In an earlier version of JShell, a method was predefined printf, but it was removed from earlier access builds. You can, of course, define your own printf method:

jshell> void printf(String format, Object... args) { System.out.printf(format, args); }

Or you can get the printing methods that were in previous versions by running JShell with:

jshell --start DEFAULT --start PRINTING

(If you use only --start PRINTING, you will not receive the default import.)

. JDK-8172102 Java b2e915d476be, .

+7

jshell? , PrintStream.

printf :

jshell> private  void printf(String s) { System.out.println(s); }

:

jshell> printf("test")
test
+1

Java - - , , . printf - PrintStream, PrintStream.

Java, System.out System.err, PrintStream, System.out.printf() System.err.printf(), plain printf() , jshell , printf().

+1

:

jshell> /set start -retain DEFAULT PRINTING

(you need to install this time. Next time you can just run jshell without any arguments). See the official jshell documentation .

0
source

Source: https://habr.com/ru/post/1676438/


All Articles