Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Compile and Run Java programs that include a Import with Sublime Text 2?

I have Windows 7, not OS X.

I configured my Sublime Text 2 with that turorial:

Compile and Run Java programs with Sublime Text 2

It works fine with this code:

enter image description here

But if i want to compile & run Code with an import, it doesn't work anymore.

enter image description here

So my Question is how to configure this batch code, that i can compile & run java code which includes imports ?

@ECHO OFF
cd %~dp1
ECHO Compiling %~nx1.......
IF EXIST %~n1.class (
DEL %~n1.class
)
javac %~nx1
IF EXIST %~n1.class (
ECHO -----------OUTPUT-----------
java %~n1
like image 935
Hidden Avatar asked May 23 '13 14:05

Hidden


2 Answers

I've been using the following setup for running java in sublime 2, and I just tested the import function and it worked fine:

Make the bat file with the following, and save it anywhere in your PATH. I suggest C:\Program Files\Java\jdk*\bin\ to keep everything together.

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

then edit C:\Users\your_user_name\AppData\Roaming\Sublime Text 2\Packages\Java\JavaC.sublime-build, the contents will be

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

replace "javac" with the name of your bat file (for instance, javacexec.bat) and save it.

Now you should be able to run it using ctrl+b.

like image 140
AsyncMoksha Avatar answered Oct 26 '22 18:10

AsyncMoksha


This package for sublime text 2 solved my problem.

https://github.com/psychowico/SublimeJavaCompiler

Features:

  • JavaC: Compile Current File
  • JavaC: Compile & Run Current File
  • JavaC: Compile Current Project
  • JavaC: Compile & Run Current Project
  • JavaC: Generate Jar Package For Project
  • JavaC: Generate & Run Jar Package For Project
  • JavaC: Clear Project
like image 28
Hidden Avatar answered Oct 26 '22 16:10

Hidden