I am trying Sublime text to learn Java using this configuration in the sublime-build
{
"cmd": "javac ${file_name} && java ${file_base_name}",
"shell": true
}
Everything goes fine when compiling and running a file. The problem arises when creating packages (package). Now it just compiles and creates the .class file but it doesn't run the file anymore.
That is the first problem I have now. If you can help me solve it or clarify why I get this error I will be very grateful.
Compile and Run Java Applications Including Packages
Well, the .sublime-build file does run the 2 commands:
javac \"$file_name\"
and then thejava \"$file_base_name\"
, which is fine when both the .class and .java files are mixed up in the same directory (which is not recommended).To compile with packages, the paths to the binaries and sources must be specified, for example, on the command line and assuming that the project is organized like this:
Create a variable for the classpath:
$ export CLASSPATH=~/miProyecto/bin
Then the variable for the sources:
$ export SOURCES=~/miProyecto/src/com/java
After the compilation:
$ javac -sourcepath com -d ${CLASSPATH} ${SOURCES}/*/*.java
Finally the execution:
$ java com/java/prueba/Prueba
The 4 previous commands are the ones that you must implement in the .sublime-build file
My setup:
Important notes:
Tested in UNIX environments with no problems.
I based my configuration on the Eclipse directory system, that is, organizing the .class packages in a 'bin' directory and the .java packages in 'src'.
Execution:
As a result, a simple frame and a thread that prints the status of a count to the console.
Tools Used:
When you compile with package, it also creates the package folders, so it is likely that you are pointing the execution to the root and not to the correct path.
As you can check if you compile the code with the JavaC tool that comes by default SublimeText will compile your code without any problem. (But as you already know, it will not be executed by something that is always better to do with a Terminal)