In Emacs use a Lisp function to run the Java program the current file correspond to.
(defun java-run-current-file ()
"Runs the java program the current file correspond to"
(interactive)
(shell-command
(concat "java "
(file-name-sans-extension
(file-name-nondirectory (buffer-file-name))))))
It works by stripping the current file name of its path and extension and using it as an argument to java
which is run from the path where the file is at. The problem with this approach is that if the current file is part of a package then
- the argument to
java
has to be prefixed with the package name and a dot, and java
has to be run from the directory containing the package.
So for example if the file is file.java and the package name is pkg java
is called as java pkg.file
from the directory containing the directory pkg (the parent directory of the directory where file.java is placed).
How can I modify the function to be aware of packages and construct the argument to java
accordingly? I guess one might solve this by searching the current file for a package declaration, such as
package pkg;
and if it finds one it uses that package name to call java
appropriately.