JShell控制台中导入自定义类

JShell控制台中导入自定义类

本文介绍了如何在intellij JShell控制台中导入自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用新的intellij Jshell控制台(在此处介绍



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

  Test2.test(); 

我收到以下错误

 C:\Program Files\Java\jdk-9.0.1\bin\java--add-modules java.xml.bind -classpathC:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.1\lib\\\\\\ .jarcom.intellij.execution.jshell.frontend.Main 

错误:找不到符号
符号:变量Test2
location:class
Rejected Test2.test ()

我是否需要为JShell配置以识别我的自定义类?

我已将其设置为使用项目的类路径。



代码。



Jshell控制台和下面的错误。



编辑



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



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



解决方案

假设您有以下项目结构:





以及以下代码:





确保在项目设置中设置库:文件 - >项目结构 - >库



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





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




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.

解决方案

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:

这篇关于如何在intellij JShell控制台中导入自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 03:34