如何在intellij JShell控制台

时间:2018-01-08 03:44:49

标签: java intellij-idea jshell

我正在使用新的intellij Jshell控制台(在此处介绍https://blog.jetbrains.com/idea/2017/09/java-9-and-intellij-idea/

我创建了一个简单的类文件Test2.java

public class Test2 {

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

JShell控制台能够在提示中找到方法 enter image description here

当我尝试在intellij jshell控制台上运行它时(工具> Jshell控制台)

Test2.test();

我收到以下错误

"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()

我有什么必须配置JShell来识别我的自定义类吗?
我已将其设置为使用项目的类路径。

代码。
enter image description here

Jshell控制台和下面的错误。 enter image description here

修改:

我还尝试将代码移动到一个包中,并按照用户@NullPointer的建议在Jshell中导入它。

同样的错误仍然存​​在,它也给了我"错误:包angelapps.java不存在"错误。

enter image description here

1 个答案:

答案 0 :(得分:8)

假设你有以下项目结构:

enter image description here

以下代码:

enter image description here

确保在项目设置中设置库:File -> Project Structure -> Libraries

确保在此处使用您的输出位置(生成类文件的位置)!它可能因构建系统(目标/类别或出/生产等)而异。

enter image description here

它应该给你正在寻找的结果:

enter image description here