Re: How to Compile Java from MacVim and Show Output in Terminal

392 views
Skip to first unread message

Xiao G. Wu

unread,
Jul 9, 2012, 1:37:16 AM7/9/12
to vim...@googlegroups.com
I would offer that you write to file,
type :shell
javac ?.java
java ?
type exit to return to the editor

Sent from my iPad

On Jul 7, 2012, at 1:25 PM, David Gillooly <david.g...@gmail.com> wrote:

> I am taking a beginning Java class that just uses a text editor and shell to create java files,compile them and run them from a terminal that shows the results. This is basic java stuff. Beginning java for idiots level!
>
> I want to use MacVim to create, compile, and run the Java exercises and examples.
>
> I can write java from MacVim and get the proper highlighting etc. However I have not been able to figure out how to compile the editor to allow me to compile and run the programs.
>
> What does one have to do? [make does't work for me]
>
> Thanks..Dave
>
> --
> You received this message from the "vim_mac" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php

Peter Palmreuther

unread,
Jul 9, 2012, 2:53:42 AM7/9/12
to vim...@googlegroups.com
Hi,

Am 07.07.2012 um 18:30 schrieb David Gillooly:
>
> I am taking a beginning Java class that just uses a text editor and shell to create java files,compile them and run them from a terminal that shows the results. This is basic java stuff. Beginning java for idiots level!
>
> I want to use MacVim to create, compile, and run the Java exercises and examples.
>
> I can write java from MacVim and get the proper highlighting etc. However I have not been able to figure out how to compile the editor to allow me to compile and run the programs.
>
> What does one have to do? [make does't work for me]

Create or open your Java source code file in MacVim, if necessary use

:setlocal filetype=java

to get proper syntax highlighting and finally

:setlocal makeprg=javac\ %

Now when you want to compile your Java class just type

:make

It will execute "javac YourClass.java" (assuming your file name is "YourClass.java").
You'll get a list of warnings or errors, if any, the same way vim would present it for a C project.

To execute your Java program type

:!java -classpath . YourClass

All this is for really simple, almost stupid, Java classes.
If things turn more complex you could try to extend 'makeprg' by arguments passed to 'javac' using '\ ' to separate them.
But most probably it's going to be easier writing a small 'Makefile' and putting it next to your source code files.

A simple one for example could like similar to this:

===== 8>< ==========
#!/usr/bin/make

SOURCES = $(wildcard *.java)
CLASSES = $(patsubst %.java, %.class, $(SOURCES))
MAINCLASS = YourClass

CLASSPATH = .
JAVA_OPTS = -classpath $(CLASSPATH)
JAVAC_OPTS = -classpath $(CLASSPATH)

DEFAULT: all

%.class: %.java
javac $(JAVAC_OPTS) $^

all: $(CLASSES)

run: all
java $(JAVA_OPTS) $(MAINCLASS)

clean:
@rm $(CLASSES)
===== 8>< ==========

With this (remember to replace "YourClass" with an appropriate value) Makefile next to your .java file(s) you should be able to open the source code in MacVim and type

:make

without any changes to 'makeprg'.
If you need to link additional libraries you only need to modify the Makefile and change 'CLASSPATH = .' appropriately (e.g. 'CLASSPATH = .:special.jar')
If you need to provide additional or special parameters to 'javac' or 'java' you'd only need to adjust 'JAVAC_OPTS' or 'JAVA_OPTS' appropriately.

But with all this information provided, remember: MacVim is not, and cannot replace, a full blown Java-IDE. There're significant differences.
Don't get me wrong, you CAN develop a Java project using MacVim as your only editor and you CAN have some (to 'a lot') of comfort doing so.
But usually a Java-IDE can provide much more, so up form a certain point it might be worth a thought to advance.
A step "in the middle" could be using 'Maven' for building your project. This way you could still stick with MacVim, while making use of Mavens great project and configuration management capabilities.
--
Best regards,

Peter

David Gillooly

unread,
Aug 23, 2012, 6:09:14 PM8/23/12
to vim...@googlegroups.com

Thanks for the assistance..Dave

David Gillooly

unread,
Aug 23, 2012, 6:12:03 PM8/23/12
to vim...@googlegroups.com

Thanks for the assistance. I am working OK now.

Yes I agree that an IDE is the way to go long term. I have looked at Eclipse and NetBeans and for me either one would work fine. Eclipse seems to support Android well but I noticed that NetBeans has added Android support too.

Thanks..Dave

Reply all
Reply to author
Forward
0 new messages