Setting Sublime Text 3 to run specific Java version
- On Sublime Text 3, go to "Tools" > "Build System" > "New Build System..."
- Paste the following piece of code. Edit the
path
setting accordingly. I'm using Zulu JDK.
{
"cmd": ["javac", "$file_name", "&&", "java", "$file_base_name"],
"selector": "source.java",
"file_regex": "^\\s*File \"(...*)\", line ([0-9]*)",
"path": "C:\\Program Files\\JDK-Zulu\\zulu-7\\bin",
"shell": true
}
- Save the setting with extension
.sublime-build
. I named mineJava7 Run.sublime-build
. - After saving, it should appear in your Build System list. Select that and now when you Build, it will use Java 7 (or whatever Java version you set it to be).
To validate, you can use System.out.println(System.getProperty("java.version"));
Sublime Text 2 build system to compile & run Java in a new Terminal/Command Prompt window?
Here's the "polite" (read: short and readable) version of what I did to make this work.
- This is a starting point only. Full impl is a blog post, not an answer.
- Assumes: OS X, xterm, no package hierarchy, etc.
- Package/project stuff is relatively straight-forward, but IMO awkward.
- I don't have a complete solution that's cross-OS or that takes weird directories into account.
- My real version makes some assumptions that may or may not work for the rest of the world.
- My real version uses Ant or Maven, which solves many problems, but not all.
- Some of this can be wrapped up in the sublime-build file, but…
- …for me it's easier this way because of other stuff not shown here.
Nutshell (Simplification): compile and run through a shell script in order to get a new window.
Script
cd $1
/usr/bin/javac $2
/usr/X11/bin/xterm -e "/bin/bash -c \"/usr/bin/java $3; echo 'Press ENTER to quit...'; read line\""
JavaC.sublime-build
{
"cmd": ["~/bin/run-java.sh $file_path $file $file_base_name"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"path": "/usr/bin/java",
"selector": "source.java",
"shell": true
}
In real life it's a bit more complex.
All this said, I never really do anything with console input in Java proper; I do it via either a Groovy or JRuby REPL, or allow stubbing of input/output sources/destinations, or… but not in Java, and not from Sublime Text 2–I use an IDE for Java development. Anything else is a waste of my time, even for short, experimental stuff.
Related Topics
How to Set Java_Home Path on Ubuntu
Problems with Java3D Lib Configuration
Java Exec() Does Not Return Expected Result of Pipes' Connected Commands
Can't Start Cassandra (Single-Node Cluster on Centos7)
Java Getlocalhost() Unknownhostexception /Etc/Hosts File Differs Linux API
Java.Lang.Runtime Exception "Cannot Run Program"
Serial Port Identification with Java on Ubuntu
Java - Io Bound Thread - 1:1 Threading Model
Java System Preferences Under Different Users in Linux
Swt Browser No More Handles Error
Find Directory for "Application Data" on Linux and MACintosh
How to Store Unix Permissions in a Zip File (Built with Apache Ant)
How to Get Desktop Class Supported Under Linux
Execute a Linux Terminal Command in Java
Installing Jdk8 on Ubuntu- "Unable to Locate Package" Update Doesn't Fix
How to Resolve "Could Not Find Jni", Building Opencv on Raspberry Pi