我创建了一个项目,并将类名命名为与文件名相同。假设我保存了该项目并想要运行它。我如何通过emacs来实现这一点?我也安装了jdk。
我有一些从尝试Java时留下的技巧。
这个技巧适用于由单个文件组成的最简单程序:
(defun java-eval-nofocus ()
"run current program (that requires no input)"
(interactive)
(let* ((source (file-name-nondirectory buffer-file-name))
(out (file-name-sans-extension source))
(class (concat out ".class")))
(save-buffer)
(shell-command (format "rm -f %s && javac %s" class source))
(if (file-exists-p class)
(shell-command (format "java %s" out) "*scratch*")
(progn
(set (make-local-variable 'compile-command)
(format "javac %s" source))
(command-execute 'compile)))))
(defun ant-compile ()
"Traveling up the path, find build.xml file and run compile"
(interactive)
(save-buffer)
(with-temp-buffer
(while (and (not (file-exists-p "build.xml"))
(not (equal "/" default-directory)))
(cd ".."))
(set (make-local-variable 'compile-command)
"ant -emacs")
(call-interactively 'compile)))
你可能可以在JDEE中找到这些的等效物,如果你能设置好它们(我自己没做到)。
以下是我使用的关键绑定:
(define-key java-mode-map [C-f5] 'java-eval-nofocus)
(define-key java-mode-map [f5] 'ant-compile)
SCons
来构建Java项目,如果需要运行它们,我只需从Emacs shell中运行即可。曾经有JDEE
项目,其中包括各种调试器和其他便利功能,但我认为它在多年前已被放弃。除此之外,我认为还有编辑和运行Ant和Maven项目的模式,你可以看一下。 - user797257