reijz
reijz

Reputation: 343

How to use vim to compile Java when classes are packaged

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?

Upvotes: 3

Views: 1576

Answers (5)

César Navejar
César Navejar

Reputation: 1

I'm using Arch Linux, java17-openjdk and of course Vim. In the same folder I have foo.java and Bar.java

// Bar.java
public class Bar {
    String webSite = "Stack Overflow";
}

// foo.java
public class foo {
    public static void main(String[] args) {
        Bar myWebSite = new Bar();
        System.out.println(myWebSite.webSite);
    }
}

In bash, when I run java foo.java, appears the error: "cannot find symbol". The correct way is to compile both java files: javac foo.java bar.java And it will generate two files .class: foo.class & bar.class. We just run the new file where the main class is, in this case, java foo (without .class), bash is going to show: Stack Overflow

Upvotes: 0

reijz
reijz

Reputation: 343

I just find the solution by accident. Since I am using MacVim with graphic interface in Mac OS X, I open the folder ~/proj/src/com/example/package/ in finder and then open the file foo.java by double click. The vim regards that we are in ~/proj/src/com/example/package/.

To make the "compiling while saving" work, just use command :cd ~/proj/bin inside of vim to inform vim to do everything as if we are in ~/proj/bin. Now it just works!

BTW, I installed spf13-vimrc.

Upvotes: 0

gustafc
gustafc

Reputation: 28895

Like the others here, I'd recommend using a real build tool instead of vim hacks. A nice alternative to Maven, is sbt (examples use Scala, but it works just as well for Java) which required less setup - no need for POM files or suchlike unless you actually want to pull dependencies from a Maven repo.

Once everything is set up, you can start sbt, type ~ compile and it will rebuild your project when file changes are detected. (Similarly, you can use ~ test to rebuild and run all tests whenever files are changed.)

Upvotes: 0

Austin R
Austin R

Reputation: 795

I had a similar problem recently, so here's what I did:

Instead of having Vim automatically compile Java on save, I wrote a script that does exactly the compile I want, and then I mapped a keystroke in my vimrc to run that compile script whenever I want.

So for example here's part of the script (I'm on a Windows 7 machine, but it shouldn't be too hard to adapt)

set projectcp=..\classes;..\classes\lib\jackcess-1.2.7.jar;..\classes\lib\commons-logging.jar;..\classes\lib\commons-lang-2.0.jar;..\classes\lib\mail.jar
set compiledir=..\classes

echo Compiling authentication\LDAP_Authenticator.java ...
javac -d %compiledir% authentication\LDAP_Authenticator.java

echo Compiling data\ProgramData.java ...
javac -d %compiledir% data\ProgramData.java

echo Compiling data\Serializer.java ...
javac -cp %projectcp% -d %compiledir% data\Serializer.java

echo Compiling data\EmailEntry.java ...
javac -cp %projectcp% -d %compiledir% data\EmailEntry.java

And in my vimrc I have this line which automatically saves the current file then runs my compile script every time I press rr

nnoremap <leader>rr :w<cr>:!compile-and-run.bat<cr>


Although yours would be:

nnoremap <leader>rr :w<cr>:!PATH/TO/SCRIPT.sh<cr>


Or perhaps on a mac you would have to do

nnoremap <leader>rr :w<cr>:!bash PATH/TO/SCRIPT.sh<cr>

Upvotes: 0

Ingo Karkat
Ingo Karkat

Reputation: 172758

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.)

Upvotes: 2

Related Questions