Java控制台输入Sublime Text 2? [英] Java console input in Sublime Text 2?

查看:189
本文介绍了Java控制台输入Sublime Text 2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近切换到sublime text 2,但我似乎找不到任何插件/资源,这将允许我实现java控制台输入到编辑器。我设法使它编译和执行java文件,但每当我的代码需要任何输入(如扫描仪输入),代码不编译,我得到一个错误。



我已经看到了解决方案,使这发生在python,但haven'y设法找到任何关于Java。

解决方案

Okay,我已经找出了一个完整和完美的解决方案,这个Run java in Sublime问题,我只在Windows 7中测试这个。



按照以下步骤,您将在sublime - JavaC和JavaC_Input中有2个构建系统。




  • JavaC可让您运行不需要用户输入的代码,并在sublime的终端模拟器中显示结果


  • JavaC_Input允许您在单独的终端窗口中运行需要用户输入的代码,它能够接受用户输入。您还可以在此构建系统中运行非输入需要的代码,因此,如果您不介意弹出窗口,则可以直接使用此构建系统,不要切换。




您可以在工具 - >构建系统之间切换构建系统。您可以使用ctrl + b编译并运行代码。



这里是实现这一步的步骤:



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



JavaC构建系统设置



1,使用以下代码创建bat文件,并将其保存在C:\Program Files \Java \jdk * \bin\将所有内容保存在一起。将文件命名为javacexec.bat。

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

2,然后编辑C:\Users \ your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\JavaC.sublime-build(如果没有,创建一个),内容将是

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

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

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



2,转到C:\ Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\,然后使用以下内容创建名为JavaC_Input.sublime-build的文件

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

3,使用以下代码创建一个bat文件,并将其保存在C:\Program Files \Java\jdk * \\ bin \保持一切。将文件命名为javacexec_input.bat。

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


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.

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

解决方案

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.

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

  • "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" 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.

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

Here are the steps to achieve this:

(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" build system setup

1, Make a bat file with the following code, and save it under C:\Program Files\Java\jdk*\bin\ to keep everything together. Name the file "javacexec.bat".

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

2, Then edit C:\Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\JavaC.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" build system setup (mainly the same as @lac_dev 's answer)

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

2, Go to C:\Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\, 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, Make a bat file with the following code, and save it under C:\Program Files\Java\jdk*\bin\ to keep everything together. Name the file "javacexec_input.bat".

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

这篇关于Java控制台输入Sublime Text 2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆