devdiva2017
devdiva2017

Reputation: 11

JShell Edit Pad Not Executing / Running Code Snippets

I am very new to jshell and Java and am having an issue with the jshell edit pad not accepting or transferring code snippets to jshell. When I enter the code snippet below and press "Accept" and "Exit" nothing is happening. No error messages are displayed in the jshell terminal window and I do not get the window displayed on my screen. The code seems not to be running.

import javax.swing.*;

JFrame window = new JFrame("Another New Window");
JPanel panel = new JPanel();
JButton button = new JButton("Click me!");

panel.add(button);
window.add(panel);
window.setSize(300,100);
button.addActionListener(e -> System.out.println("Ouch! You clicked me!"));
window.setVisible(true);

However, when I enter this same code snippet directly into the jshell window (command prompt), the window appears as it should. What could be going wrong? What mistakes am I making?

I have noticed that I am able to get the results of simple operations, such as 2+2; to show the answer in the jshell console window after pressing "Accept", but actual code entered into the default edit pad seems not to run at all.

I am running Windows 10 and I have the latest versions of JDK 8 and 10 installed.

Thank you for your assistance!

Upvotes: 1

Views: 960

Answers (3)

Jay
Jay

Reputation: 1

I also was able to eventually get it working "line by line" as earlier described. Then I rebooted the machine and got it working the first time using jdk 11.0.

Upvotes: 0

Chaos7703
Chaos7703

Reputation: 691

This is not an answer so much as more information. I will update this if I find a fix.

I am working through this same exercise in "Learn Java The Easy Way" Ch 3 and found the same problem. That is entering 1 line at a time in JShell opened the JFrame as expected.

jshell> import javax.swing.JFrame;
jshell> JFrame f = new JFrame("Hello");
jshell> f.setSize(300,300);
jshell> f.setVisible(true);

JShell CLI input works

Then it's closed using /reset and open the JShell Edit Pad with /edit & enter the original snippet, click "Accept," and then "Exit," but nothing happens. When I run /edit again only the import is there. I retyped & copied the script before exiting again and still nothing. However, when I opened /edit again, line 1 and 2 were there. So I pasted the whole script again, exited again, & ran /edit again. This time lines 1 - 3 were there. I repeated this and each time one more line was executed & one more showed up in the script when running /edit again, until the tenth times when the window opened as was expected.Edit Pad parsing/executing only one additional line on each exit.

This is not an answer or fix, but I thought it might give someone else a clue to what is/isn't happening. In my case I wonder if it might be cause by mixed configuration or a bug in the OpenJDK. In the meantime, I'll move on in the book since this is just "proof of concept" material anyway.

  • Fedora 29
  • JShell -- Version 11.0.4
  • java -version
  • openjdk version "1.8.0_212"
  • OpenJDK Runtime Environment (build 1.8.0_212-b04)
  • OpenJDK 64-Bit Server VM (build 25.212-b04, mixed mode)
  • javac 11.0.4

Upvotes: 0

gbdcool
gbdcool

Reputation: 982

I've JDK 12 and JRE 8 installed. It is working as expected. To start fresh JShell Edit Pad, just make sure you do

/reset

before

/edit

to open the JShell Edit Pad.

enter image description here

Try with JDK 12.

Upvotes: 0

Related Questions