简体   繁体   English

如何在intellij JShell控制台中导入自定义类

[英]How to import a custom class in intellij JShell console

I am using the new intellij Jshell console (introduced here https://blog.jetbrains.com/idea/2017/09/java-9-and-intellij-idea/ ) 我正在使用新的intellij Jshell控制台(在此处介绍https://blog.jetbrains.com/idea/2017/09/java-9-and-intellij-idea/

I created a simple class file Test2.java 我创建了一个简单的类文件Test2.java

public class Test2 {

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

The JShell console is able to find the method in the hints JShell控制台能够在提示中找到该方法 在此输入图像描述

when i try to run this on intellij jshell console (Tools>Jshell Console) 当我尝试在intellij jshell控制台上运行它时(工具> Jshell控制台)

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? 有什么东西我必须配置JShell来识别我的自定义类?
I have set it to use the class path of my project. 我已将其设置为使用项目的类路径。

The codes. 代码。
在此输入图像描述

The Jshell console and the error below. Jshell控制台和下面的错误。 在此输入图像描述

Edit: 编辑:

I've also tried to move the codes into a package and importing it in Jshell as suggested by user @NullPointer. 我还尝试将代码移动到一个包中,并按照用户@NullPointer的建议在Jshell中导入它。

The same error persists and it also gives me "ERROR: package angelapps.java does not exist" error. 同样的错误仍然存​​在,它也给我“错误:包angelapps.java不存在”错误。

在此输入图像描述

Let's say you have following structure of project: 假设你有以下项目结构:

在此输入图像描述

and following code: 以下代码:

在此输入图像描述

Make sure to set Libraries in Project Settings: File -> Project Structure -> Libraries 确保在项目设置中设置库: 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: 它应该给你正在寻找的结果:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM