-1

I'm using GVim. I created a file caled InsertionSort.java and this is what is inside the file:

public class InsertionSort {
    public static void main (String[] args) {
        System.out.println("Hello World");
    }
}

I then opened up terminal and did

sudo apt-get install default-jdk

to install javac. Next, I went back to GVim and did:

:!javac %

to run the current file. When I did this, it said:

!javac /Documents/Java/InsertionSort.java
Press ENTER or type command to continue

and when I press ENTER, it goes back to InsertionSort.java. It doesn't print anything. I looked at this post: Compiling Java code in Vim more efficiently and the highest rated answer said to add these to my .vimrc file:

autocmd Filetype java set makeprg=javac\ %
set errorformat=%A%f:%l:\ %m,%-Z%p^,%-C%.%#
map <F9> :make<Return>:copen<Return>
map <F10> :cprevious<Return>
map <F11> :cnext<Return>

After adding the above lines to my .vimrc file, I reopend GVim / InsertionSort.java and pressed F9 and it said:

!javac Documents/Java/InsertionSort.java 2>&1| tee /tmp/vcqcE1x/8

:copen

Press ENTER or type command to continue

and when I pressed ENTER, it opened up a new GVim file called:

[Quickfix List] :javac Documents/Java/InsertionSort.java

and the file did not have anything in it (It didn't say "Hello World"). How do I make it run the file so that it displays "Hello World"?

Community
  • 1
  • 1
SilentDev
  • 20,997
  • 28
  • 111
  • 214

3 Answers3

1

I think you may be mixing up the java and javac programs. javac compiles *.java source files into *.class bytecode files. java takes compiled *.class bytecode files and runs them. So if your goal is to compile and run a file called Example.java, you'll need to do this:

javac Example.java
java Example
Sam Estep
  • 12,974
  • 2
  • 37
  • 75
1

javac is the compiler, not the executor. It will compile your source code into a class file which then needs to be run with java rather than javac.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

If your system has jdk you should be able to compile your java file using javac program.java When your java file is successfully compiled,JVM converts java file into .class file. Now you can run your .class file using java program

gkaka
  • 61
  • 1
  • 6