Compile and Run Java Projects

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Compile and Run Java Projects

Luis Henriquez-Perez
I am using vim to code my java projects. I've been noticing I've been jumping back to the terminal a lot to compile and run my code. So I want to create a function that does all this for me (and gets rid of the class files afterwards). The code below is my attempt. Could someone let me know how I can fix my code?

func! CompileFolderJava()

        " compile all java files in folder of the current buffer
        :!javac "%:p:h" . "/*.java"       "  javac path/name/to/current/buffer/directory/*.java


        " run Main java file in that folder
        :!java "%:p:h" . ".Main"            " java path/name/to/current/buffer/directory.Main

        " delete all the .class files in that folder
        :!rm "%:p:h" . ".class"

        " :echo "Done"
endfunc

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compile and Run Java Projects

Luis Henriquez-Perez
On Monday, October 17, 2016 at 6:08:35 PM UTC-4, Luis Henriquez-Perez wrote:

> I am using vim to code my java projects. I've been noticing I've been jumping back to the terminal a lot to compile and run my code. So I want to create a function that does all this for me (and gets rid of the class files afterwards). The code below is my attempt. Could someone let me know how I can fix my code?
>
> func! CompileFolderJava()
>
> " compile all java files in folder of the current buffer
> :!javac "%:p:h" . "/*.java"       "  javac path/name/to/current/buffer/directory/*.java
>
>
> " run Main java file in that folder
> :!java "%:p:h" . ".Main"            " java path/name/to/current/buffer/directory.Main
>
> " delete all the .class files in that folder
> :!rm "%:p:h" . ".class"
>
> " :echo "Done"
> endfunc
I managed to get the compiling and the removing to work. For running, I think I have to be in the parent directory of the file. The format needs to be: java ParentDir.Main

func! CompileFolderJava()
        " compile all java files in folder
        :!javac %:p:h/*.java

        " a java thing I have to be in this directory to call java main
        :cd ..
        " run all the java files in folder
        :!java %:h.Main

        " delete all the .class files in folder
        :!rm %:p:h/*.class

        " :echo "hello"
endfunc

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compile and Run Java Projects

Eric Christopherson
On Mon, Oct 17, 2016, Luis Henriquez-Perez wrote:

> On Monday, October 17, 2016 at 6:08:35 PM UTC-4, Luis Henriquez-Perez wrote:
> > I am using vim to code my java projects. I've been noticing I've been jumping back to the terminal a lot to compile and run my code. So I want to create a function that does all this for me (and gets rid of the class files afterwards). The code below is my attempt. Could someone let me know how I can fix my code?
> >
> > func! CompileFolderJava()
> >
> > " compile all java files in folder of the current buffer
> > :!javac "%:p:h" . "/*.java"       "  javac path/name/to/current/buffer/directory/*.java
> >
> >
> > " run Main java file in that folder
> > :!java "%:p:h" . ".Main"            " java path/name/to/current/buffer/directory.Main
> >
> > " delete all the .class files in that folder
> > :!rm "%:p:h" . ".class"
> >
> > " :echo "Done"
> > endfunc
>
> I managed to get the compiling and the removing to work. For running, I think I have to be in the parent directory of the file. The format needs to be: java ParentDir.Main

Well, what's going wrong with it? I would suggest, though, that instead
of changing directories you use the classpath (-cp) option to tell Java
where to find the Main class. E.g.

    :!java -cp %:p:h Main

>
> func! CompileFolderJava()
> " compile all java files in folder
> :!javac %:p:h/*.java
>
> " a java thing I have to be in this directory to call java main
> :cd ..
> " run all the java files in folder
> :!java %:h.Main
>
> " delete all the .class files in folder
> :!rm %:p:h/*.class
>
> " :echo "hello"
> endfunc

--
        Eric Christopherson

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compile and Run Java Projects

Luis Henriquez-Perez
In reply to this post by Luis Henriquez-Perez
On Monday, October 17, 2016 at 6:08:35 PM UTC-4, Luis Henriquez-Perez wrote:

> I am using vim to code my java projects. I've been noticing I've been jumping back to the terminal a lot to compile and run my code. So I want to create a function that does all this for me (and gets rid of the class files afterwards). The code below is my attempt. Could someone let me know how I can fix my code?
>
> func! CompileFolderJava()
>
> " compile all java files in folder of the current buffer
> :!javac "%:p:h" . "/*.java"       "  javac path/name/to/current/buffer/directory/*.java
>
>
> " run Main java file in that folder
> :!java "%:p:h" . ".Main"            " java path/name/to/current/buffer/directory.Main
>
> " delete all the .class files in that folder
> :!rm "%:p:h" . ".class"
>
> " :echo "Done"
> endfunc
The solution of  " :!java -cp %:p:h Main"  did not work out.

I think this might be because my java files all have the following line:
Package myjavafiles;

as of yet the only way I've found to compile and run it from the terminal (and not from vim)
is by being in the parent directory of myjavafiles and doing:

javac myjavafiles/*.java
java myjavafiles.Main

I've found that I can specify the directory of the java files I want to compile in the "javac" command. But when the "java" command will only work when I'm in the directory that has the folder of java files.

I came to this conclusion after doing the following in vim from myjavafiles directory:

// succesful
: cd ..
: !javac myjavafiles/*.java
: !java myjavafiles.Main

// not successful
: !javac *.java          <---worked
: !java Main             <----did not work

// suprisingly not successful
: cd ..
: !javac myjavafiles/*.java
: !java ~/path/to/myjavafiles.Main

Because of these tests I think that perhaps I need to change directories to compile. But I'm not sure how to get only the folder name "myjavafiles" instead of the whole path.

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compile and Run Java Projects

Eric Christopherson
On Mon, Oct 17, 2016, Luis Henriquez-Perez wrote:

> On Monday, October 17, 2016 at 6:08:35 PM UTC-4, Luis Henriquez-Perez wrote:
> > I am using vim to code my java projects. I've been noticing I've been jumping back to the terminal a lot to compile and run my code. So I want to create a function that does all this for me (and gets rid of the class files afterwards). The code below is my attempt. Could someone let me know how I can fix my code?
> >
> > func! CompileFolderJava()
> >
> > " compile all java files in folder of the current buffer
> > :!javac "%:p:h" . "/*.java"       "  javac path/name/to/current/buffer/directory/*.java
> >
> >
> > " run Main java file in that folder
> > :!java "%:p:h" . ".Main"            " java path/name/to/current/buffer/directory.Main
> >
> > " delete all the .class files in that folder
> > :!rm "%:p:h" . ".class"
> >
> > " :echo "Done"
> > endfunc
>
> The solution of  " :!java -cp %:p:h Main"  did not work out.
>
> I think this might be because my java files all have the following line:
> Package myjavafiles;
>
> as of yet the only way I've found to compile and run it from the terminal (and not from vim)
> is by being in the parent directory of myjavafiles and doing:
>
> javac myjavafiles/*.java
> java myjavafiles.Main
>
> I've found that I can specify the directory of the java files I want to compile in the "javac" command. But when the "java" command will only work when I'm in the directory that has the folder of java files.
>
> I came to this conclusion after doing the following in vim from myjavafiles directory:
>
> // succesful
> : cd ..
> : !javac myjavafiles/*.java
> : !java myjavafiles.Main
>
> // not successful
> : !javac *.java          <---worked
> : !java Main             <----did not work
>
> // suprisingly not successful
> : cd ..
> : !javac myjavafiles/*.java
> : !java ~/path/to/myjavafiles.Main
>
> Because of these tests I think that perhaps I need to change directories to compile. But I'm not sure how to get only the folder name "myjavafiles" instead of the whole path.

Try %:p:h:t to get just 'myjavafiles'.

The help for %'s modifiers is can be accessed at :help
filename-modifiers.

--
        Eric Christopherson

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compile and Run Java Projects

Luis Henriquez-Perez
In reply to this post by Luis Henriquez-Perez
On Monday, October 17, 2016 at 6:08:35 PM UTC-4, Luis Henriquez-Perez wrote:

> I am using vim to code my java projects. I've been noticing I've been jumping back to the terminal a lot to compile and run my code. So I want to create a function that does all this for me (and gets rid of the class files afterwards). The code below is my attempt. Could someone let me know how I can fix my code?
>
> func! CompileFolderJava()
>
> " compile all java files in folder of the current buffer
> :!javac "%:p:h" . "/*.java"       "  javac path/name/to/current/buffer/directory/*.java
>
>
> " run Main java file in that folder
> :!java "%:p:h" . ".Main"            " java path/name/to/current/buffer/directory.Main
>
> " delete all the .class files in that folder
> :!rm "%:p:h" . ".class"
>
> " :echo "Done"
> endfunc
Thank you. It worked out.

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compile and Run Java Projects

Luis Henriquez-Perez
In reply to this post by Luis Henriquez-Perez
On Monday, October 17, 2016 at 6:08:35 PM UTC-4, Luis Henriquez-Perez wrote:

> I am using vim to code my java projects. I've been noticing I've been jumping back to the terminal a lot to compile and run my code. So I want to create a function that does all this for me (and gets rid of the class files afterwards). The code below is my attempt. Could someone let me know how I can fix my code?
>
> func! CompileFolderJava()
>
> " compile all java files in folder of the current buffer
> :!javac "%:p:h" . "/*.java"       "  javac path/name/to/current/buffer/directory/*.java
>
>
> " run Main java file in that folder
> :!java "%:p:h" . ".Main"            " java path/name/to/current/buffer/directory.Main
>
> " delete all the .class files in that folder
> :!rm "%:p:h" . ".class"
>
> " :echo "Done"
> endfunc
Oh and in case anyone would like to learn from the solution I will post it too.
func! CompileFolderJava()

        " compile all java files in folder
        :!javac %:p:h/*.java

        " save the variable containing current path
        :let mypath = expand(":cd %:p:h")

        " a java thing I have to be in this directory to call java main
        :cd ..

        " run all the java files in folder
        :!java %:p:h:t.Main

        " get back to current directory
        :execute mypath

        " delete all the .class files in folder
        :!rm %:p:h/*.class
endfunc

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compile and Run Java Projects

Luc Hermitte
Hi,

You should instead use the integrated :make/quickfix feature of vim. You'll have a much better user experience.

First be sure that you have set javac as the make program. It's best done by sourcing compiler/javac.vim
-> :runtime compiler/javac.vim

I would have done it in a java ftplugin, but as I'm not a Java developer, I'm not sure this is a good default setting. So, let's put it in your function.


```vim
command! MakeAndRun :call s:CompileAndRunFolderJava()

function! s:CompileAndRunFolderJava() abort
   runtime compiler/javac.vim

   " old trick to detect compilation errors - part 1
   " --- The following code is borrowed from LaTeXSuite
   "   close the quickfix window before trying to open it again,
   "   otherwise whether or not we end up in the quickfix window
   "   after the :cwindow command is not fixed.
   cclose " used to detect errors
   let winnum = winnr()

   " compile all java files in folder of the current buffer
   make %:p:h/*.java

   " old trick to detect compilation errors - part 2
   if winnum == winnr() " => no error detected
     " run Main java file in that folder
     :!(cd .. && java %:p:h:t.Main)

     " delete all the .class files in that folder
     :!rm %:p:h/*.class
   endif

   " :echomsg "Done"
endfunction
```

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Compile and Run Java Projects

Luis Henriquez-Perez
In reply to this post by Luis Henriquez-Perez
On Monday, October 17, 2016 at 6:08:35 PM UTC-4, Luis Henriquez-Perez wrote:

> I am using vim to code my java projects. I've been noticing I've been jumping back to the terminal a lot to compile and run my code. So I want to create a function that does all this for me (and gets rid of the class files afterwards). The code below is my attempt. Could someone let me know how I can fix my code?
>
> func! CompileFolderJava()
>
> " compile all java files in folder of the current buffer
> :!javac "%:p:h" . "/*.java"       "  javac path/name/to/current/buffer/directory/*.java
>
>
> " run Main java file in that folder
> :!java "%:p:h" . ".Main"            " java path/name/to/current/buffer/directory.Main
>
> " delete all the .class files in that folder
> :!rm "%:p:h" . ".class"
>
> " :echo "Done"
> endfunc
Thank you! This also works perfectly. I'll check out help :make to learn about the user experience benefits you spoke of.

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.