Getting Input From User

Sundararajan Athijegannathan sundararajan.athijegannathan at oracle.com
Mon Feb 27 01:38:04 UTC 2017


I think print and readLine cannot be compared.

Differences:

* print is always available. Both in -scripting mode as well as normal mode.
* readLine extension (as Jim pointed out) is available only in 
-scripting mode. (scripting mode is meant "shell" style scripting with 
nashorn)
* While it is usually okay/safe to use arbitrary output/print stream and 
direct output there, read/readLine is expected read from console (meant 
for interactive user input).

That said, it should be straightforward to define your own read/readLine 
replacement function that uses ScriptContext's Reader.

-Sundar

On 26/02/17, 10:21 AM, Koray wrote:
> That expanded my horizon, thank you.
>
> However even though print() utilizes the given writer, E.G: Socket's
> OutputWriter, readLine still uses the System.in even though I use
> engine.getContext().setReader(InputStreamReader of a Socket). Would
> that be some kind of bug? Because that makes Scriptcontext.setReader()
> method kind of useless.
>
> Burak
> On 2/25/17, James Laskey<james.laskey at oracle.com>  wrote:
>> You have to be in scripting mode to to use those functions.
>> https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/shell.html.
>> It's possible to add command line options when using from java.
>> https://wiki.openjdk.java.net/display/Nashorn/Nashorn+jsr223+engine+notes
>>
>> -- Jim
>>
>> Sent from my iPhone
>>
>>> On Feb 25, 2017, at 1:41 PM, Koray<burakkoray at gmail.com>  wrote:
>>>
>>> This is exactly what I'm looking for. Is it possible to do this from
>>> inside a Java application? I have tried readLine() and readFully()
>>> methods before, but I'm guessing they are meant for the commandline
>>> tool only.
>>>
>>> Burak
>>> On 2/25/17, Jim Laskey (Oracle)<james.laskey at oracle.com>  wrote:
>>>>>> jjs -scripting
>>>> jjs>  var name = readLine("Your name>>  ");
>>>> Your name>>  Jim
>>>> jjs>  print(name)
>>>> Jim
>>>>
>>>> That sort of thing?
>>>>
>>>> — Jim
>>>>
>>>>
>>>>> On Feb 25, 2017, at 5:24 AM, Koray<burakkoray at gmail.com>  wrote:
>>>>>
>>>>> Hello,
>>>>>
>>>>> I apologize if this is not the place to ask, but I couldn't get a
>>>>> satisfying answer from anywhere else so this is kind of my last
>>>>> resort.
>>>>>
>>>>> Is there any built-in function to get input from a user when using
>>>>> NashornScriptEngine? print() method allows us to utilize both the
>>>>> default OutputStreamWriter (System.out) and any writers that we set
>>>>> using ScriptContext, but how can I make use of the reader which is
>>>>> used by the engine?
>>>>>
>>>>> Thank you,
>>>>> Burak
>>>>
>>>
>>> --
>>> saygılarımla
>>> Burak Koray Polat
>


More information about the nashorn-dev mailing list