Merge branch 'maint'
[org-mode.git] / lisp / ob-java.el
blob608e2e8858a5918b30763e015d7762b43e21392f
1 ;;; ob-java.el --- Babel Functions for Java -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Currently this only supports the external compilation and execution
27 ;; of java code blocks (i.e., no session support).
29 ;;; Code:
30 (require 'ob)
32 (defvar org-babel-tangle-lang-exts)
33 (add-to-list 'org-babel-tangle-lang-exts '("java" . "java"))
35 (defcustom org-babel-java-command "java"
36 "Name of the java command.
37 May be either a command in the path, like java
38 or an absolute path name, like /usr/local/bin/java
39 parameters may be used, like java -verbose"
40 :group 'org-babel
41 :version "24.3"
42 :type 'string)
44 (defcustom org-babel-java-compiler "javac"
45 "Name of the java compiler.
46 May be either a command in the path, like javac
47 or an absolute path name, like /usr/local/bin/javac
48 parameters may be used, like javac -verbose"
49 :group 'org-babel
50 :version "24.3"
51 :type 'string)
53 (defun org-babel-execute:java (body params)
54 (let* ((classname (or (cdr (assq :classname params))
55 (error
56 "Can't compile a java block without a classname")))
57 (packagename (file-name-directory classname))
58 (src-file (concat classname ".java"))
59 (cmpflag (or (cdr (assq :cmpflag params)) ""))
60 (cmdline (or (cdr (assq :cmdline params)) ""))
61 (full-body (org-babel-expand-body:generic body params)))
62 (with-temp-file src-file (insert full-body))
63 (org-babel-eval
64 (concat org-babel-java-compiler " " cmpflag " " src-file) "")
65 ;; created package-name directories if missing
66 (unless (or (not packagename) (file-exists-p packagename))
67 (make-directory packagename 'parents))
68 (let ((results (org-babel-eval (concat org-babel-java-command
69 " " cmdline " " classname) "")))
70 (org-babel-reassemble-table
71 (org-babel-result-cond (cdr (assq :result-params params))
72 (org-babel-read results)
73 (let ((tmp-file (org-babel-temp-file "c-")))
74 (with-temp-file tmp-file (insert results))
75 (org-babel-import-elisp-from-file tmp-file)))
76 (org-babel-pick-name
77 (cdr (assq :colname-names params)) (cdr (assq :colnames params)))
78 (org-babel-pick-name
79 (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))
81 (provide 'ob-java)
85 ;;; ob-java.el ends here