Sublime Text 2中的Java控制台输入?

2022-01-11 00:00:00 input console sublimetext2 java

我最近切换到了 sublime text 2,但我似乎找不到任何插件/资源可以让我在编辑器中实现 java 控制台输入.我已经设法让它编译和执行 java 文件,但是每当我的代码需要任何输入(如扫描仪输入)时,代码就无法编译并且出现错误.

I've recently made the switch to sublime text 2 but I cannot seem to find any plugins/resources which will allow me to implement java console inputs into the editor. I've managed to make it compile and execute java files, but whenever my code needs any input(like a scanner input), the code does not compile and I get an error.

我已经看到了在 python 上实现这一点的解决方案,但还没有设法在 Java 上找到任何东西.

I've seen solutions to make this happen for python, but haven'y managed to find anything on Java.

推荐答案

好的,我已经想出了一个完整和完美的解决方案来解决这个在 Sublime 中运行 java"的问题,我只有在 Windows 7 中对此进行了测试.

Okay, I've figured out a complete and perfect solution to this "Run java in Sublime" problem, I've only tested this in Windows 7.

按照以下步骤,您将拥有 2 个崇高的构建系统 - JavaC"和JavaC_Input".

By following the steps below, you will have 2 Build Systems in sublime - "JavaC" and "JavaC_Input".

  • JavaC"可以让你运行不需要用户输入的代码,并在sublime的终端模拟器中显示结果,既方便又好看.

  • "JavaC" would let you run code that doesn't require user input and display the results in sublime's terminal simulator, which is convenient and nice-looking.

JavaC_Input"允许您在单独的终端窗口中运行需要用户输入的代码,它能够接受用户输入.你也可以在这个构建系统中运行不需要输入的代码,所以如果你不介意弹出窗口,你可以坚持使用这个构建系统,不要切换.

"JavaC_Input" lets you run code that requires user input in a separate terminal window, it's able to accept user input. You can also run non-input-requiring code in this build system, so if you don't mind the pop-up, you can just stick with this build system and don't switch.

您可以从工具 -> 构建系统在构建系统之间切换.然后使用 ctrl+b 编译和运行代码.

You switch between build systems from Tools -> Build System. And you compile&run code using ctrl+b.

以下是实现此目的的步骤:

Here are the steps to achieve this:

(注意:请确保您已经具备java系统的基本设置:安装JDK并设置正确的CLASSPATH和PATH,我不会详细说明)

(note: Make sure you already have the basic setup of the java system: install JDK and set up correct CLASSPATH and PATH, I won't elaborate on this)

JavaC"构建系统设置

1,用以下代码制作一个bat文件,并将其保存在C: Program Files Java jdk * bin 下,以将所有内容放在一起.将文件命名为javacexec.bat".

1, Make a bat file with the following code, and save it under C:Program FilesJavajdk*in to keep everything together. Name the file "javacexec.bat".

@ECHO OFF
cd %~dp1
javac %~nx1
java %~n1

2、然后编辑 C:Usersyour_user_nameAppDataRoamingSublime Text 2PackagesJavaJavaC.sublime-build (如果没有,创建一个),内容为

2, Then edit C:Usersyour_user_nameAppDataRoamingSublime Text 2PackagesJavaJavaC.sublime-build (if there isn't any, create one), the contents will be

{
   "cmd": ["javacexec.bat", "$file"],
   "file_regex": "^(...*?):([0-9]*):?([0-9]*)",
   "selector": "source.java"
}

JavaC_Input"构建系统设置(主要与@lac_dev 的回答相同)

1、安装Cygwin [http://www.cygwin.com/]

1, Install Cygwin [http://www.cygwin.com/]

2、进入C:Usersyour_user_nameAppDataRoamingSublime Text 2PackagesJava,然后创建一个名为JavaC_Input.sublime-build"的文件,内容如下

2, Go to C:Usersyour_user_nameAppDataRoamingSublime Text 2PackagesJava, then create a file called "JavaC_Input.sublime-build" with the following content

{
"cmd": ["javacexec_input.bat", "$file"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"selector": "source.java"
}

3,用以下代码制作一个bat文件,并将其保存在C:Program FilesJavajdk*in下,以便将所有内容放在一起.将文件命名为javacexec_input.bat".

3, Make a bat file with the following code, and save it under C:Program FilesJavajdk*in to keep everything together. Name the file "javacexec_input.bat".

@echo off
javac  -Xlint:unchecked %~n1.java 
start cmd /k java -ea %~n1

相关文章