How to import a custom class in intellij JShell console

♀尐吖头ヾ 提交于 2019-12-19 00:41:41

问题


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

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.

The Jshell console and the error below.

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.


回答1:


Let's say you have following structure of project:

and following code:

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.)

It should give you result you are looking for:



来源:https://stackoverflow.com/questions/48143960/how-to-import-a-custom-class-in-intellij-jshell-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!