1

I am an okay Java programmer and have very little knowledge of Emacs lisp, but I would still like to be able to write some packages for Emacs. Is there a way or package that would allow me to write a package in Java?

12

You can't directly write an Emacs package in Java. There is currently no way to extend Emacs in Java, nor is there a Java to Elisp transpiler.

It's common, though, to have Emacs run other programs to do work for it. You could write a helper program like this in Java. Whether this makes sense depends on what exactly you intend to do.

  • 1
    What I meant is if there was a package that would run Java programs. – user5503 Feb 17 '15 at 11:05
7

Here's a little snippet that allows to run a single Java file:

(defun java-eval-nofocus ()
  "Run current Java file."
  (interactive)
  (save-buffer)
  (let* ((source (file-name-nondirectory buffer-file-name))
         (out (file-name-sans-extension source))
         (class (concat out ".class")))
    (setenv "CLASSPATH" default-directory)
    (shell-command (format "rm -f %s && javac %s" class source))
    (if (file-exists-p class)
        (message (shell-command-to-string (format "java %s" out)))
      (progn
        (set (make-local-variable 'compile-command)
             (format "javac %s" source))
        (command-execute 'compile)))))

You can simplify it for your purposes, for instance, assume you have Utility.class on your CLASSPATH, that prints some useful info like today's weather. Then to insert this info into a buffer you need:

(defun insert-weather ()
  (interactive)
  (insert
   (shell-command-to-string "java Utility")))
(global-set-key (kbd "C-c C-w") 'insert-weather)

Still, you won't be able to do anything more useful than inserting static text unless you learn some Elisp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy