17

I am using the new intellij Jshell console (introduced here https://blog.jetbrains.com/idea/2017/09/java-9-and-intellij-idea/)

I created a simple class file Test2.java

public class Test2 {

    public static String test(){
        return "Hello";
    }
}

The JShell console is able to find the method in the hints enter image description here

when i try to run this on intellij jshell console (Tools>Jshell Console)

Test2.test();

I get the following error

"C:\Program Files\Java\jdk-9.0.1\bin\java" --add-modules java.xml.bind -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.1\lib\jshell-frontend.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.1\lib\jshell-protocol.jar" com.intellij.execution.jshell.frontend.Main

ERROR: cannot find symbol
  symbol:   variable Test2
  location: class 
Rejected Test2.test()

Are there any thing I have to configure for JShell to recognise my custom class?
I have set it to use the class path of my project.

The codes.
enter image description here

The Jshell console and the error below. enter image description here

Edit:

I've also tried to move the codes into a package and importing it in Jshell as suggested by user @NullPointer.

The same error persists and it also gives me "ERROR: package angelapps.java does not exist" error.

enter image description here

Angel Koh
  • 12,479
  • 7
  • 64
  • 91
  • Could package your classes better `src/main/java//Test2` and then try running something like `import .Test2;Test2.test();` while using the classpath for *`MyFirstApp`*? – Naman Jan 08 '18 at 06:31
  • @nullpointer, I tried setting a package name for the class too, didn't work (see screenshot in edit). – Angel Koh Jan 08 '18 at 11:11

1 Answers1

20

Let's say you have following structure of project:

enter image description here

and following code:

enter image description here

Make sure to set Libraries in Project Settings: File -> Project Structure -> Libraries

Make sure to use your output location here (location where your class files are generated)! It may vary depending on build system (target/classes or out/production, etc.)

enter image description here

It should give you result you are looking for:

enter image description here

Oo.oO
  • 12,464
  • 3
  • 23
  • 45
  • 2
    Well kinda [what I meant](https://stackoverflow.com/questions/48143960/how-to-import-a-custom-class-in-intellij-jshell-console?noredirect=1#comment83268600_48143960) as well. :) – Naman Jan 08 '18 at 13:40
  • 2
    .oOo. Cool! .oOo. – Oo.oO Jan 09 '18 at 06:12
  • 1
    Doesn't work for me, I still get "ERROR: package org.thinkbigthings.jshell does not exist Rejected import org.thinkbigthings.jshell.*" I added the project to its own libraries as described, and tried restarting jshell and the IDE. – Jason Jan 12 '18 at 11:10
  • 2
    Oh, I added the project (itself, base folder) as a library, not the project output folder, and the path looked very similar. In your answer when you say "Make sure to set Libraries in Project Settings" can you be explicit about it being the output folder containing the base of your project output classes? It's kind of hard to see the exact path in the image and figure out what the corresponding path is for my project (I'm on gradle and my output path is different) – Jason Jan 12 '18 at 11:19
  • 3
    Even this is working it has some drawback. As long the JShell console is open no Java source of the project gets compiled. To make JShell aware of code changes you need to 1) close JShell console, 2) force compilation of the changed code, either compile only single files or the whole project, 3) re-open JShell console. Seems the solution to add the output directory as library to the project, to get the classes available in JShell console, comes with limitation or is not even intended to work, see [IDEA-176579](https://youtrack.jetbrains.com/issue/IDEA-176579) – SubOptimal Mar 11 '18 at 12:18