Suppose Java classes are packaged, and here is the file directory:
~/proj/bin/com/example/package/foo.class
~/proj/bin/com/example/package/foo1.class
~/proj/src/com/example/package/foo.java
~/proj/src/com/example/package/foo1.java
Class foo need to use class foo1. In terminal, here is how I can compile
cd ~/proj/bin
javac -d ./ ../src/com/example/package/foo.java
(I already set $CLASSPATH = "~/proj/bin". It seems this way only works when we cd to ~/proj/bin. )
Now using vim (I am using MacVim on Mac with spf13-vim distribution of vim plugins). When the .java file is saved by vim, it is compiled using javac automatically. However, vim regards the current folder as ~/proj/bin/com/example/package/, not ~/proj/bin. So it shows error in foo.java whenever it uses class from foo1.
How to automatically compile java upon saving when java classes are packaged?
Don't forget that Vim is a general-purpose text editor. Though plugins like the ones provided by your chosen distribution provide functionality to quickly compile individual files, once you move up to larger projects, choose an appropriate tool for the job (that e.g. properly handles dependencies).
Since this is Java, there's a wealth of tools available: Standard build tools like Ant or Maven provide all the features for project compilation you'll ever need, and can be triggered from within Vim via :make. Or, if your projects become much larger, it might make sense to switch to a fully-featured IDE like Eclipse, NetBeans, or IntelliJ IDEA to profit from the superior code navigation and debugging capabilities. (You can still use Ant or Maven underneath them, and edit files concurrently in Vim.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With