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"?
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
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
.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.