Is there any way to run a Java jar file in R? I want one of the outputs of a .jar be used in R.
You can use system to run an operating system command from R.
To run a java jar on a well-configured system, this should work:
system("java -jar /path/to/my.jar")
You can add other parameters, for example maybe the Java code takes input from a file which you write with R and pass the filename. Then output from the Java code could be written to files and then read from R. Without knowing what the Java code does we can't be more specific.
It is possible to directly interface with Java code but that requires full knowledge of the internals of the jar so you know what functions to call with what parameters. This is usually referred to as the "API" for that Java.
Otherwise, write a file, call system, read a file, is sometimes the simplest way to run code in other languages.
Java's .jar can only be run by java; so what you need is a way to run an external command from R. The command will be java, with arguments -jar and your.jar.
As far as I can tell, you need to use system function, something along the lines of
javaOutput <- system("/usr/local/bin/java -jar your.jar", intern = TRUE)
(note I have not tested that, so please do test)
Related
I'm trying to make a java program which executes java files and gives output in the text field. I've used Runtime class to compile the .java file .So how do I get the output from that newly made class file.
Runtime.getRuntime().exec("javac Y://CodeSave.java");
Runtime.getRuntime().exec("java Y://CodeSave.class>output.txt");
In the general case: exec returns a Process instance which has accessors (getOutputStream, etc.) for the I/O streams. You read from / write to those streams.
But: In your code you've used >output.txt. That's a shell feature. If you want to do it that way, you need to spawn a shell, not the java tool directly, and have the shell execute that command line. (A search for spawning/execing a shell should find you lots of examples.)
Using Runtime.exec is definitely not the right way to do it, for various reasons. Examples are that both java and javac rely on environment variables which you can't pass that way.
First of all, I'd ask myself if I really needed to do this. Compiling and executing dynamically created code is a huge security risk.
But if you're sure you need to do it, here's what I'd do.
Move your sources to a dedicated temporary folder
Use the ToolProvider api to compile your sources
Use a dynamic throwaway ClassLoader (ByteBuddy may help you there) with a SecurityManager to load and execute your code from within your application
In Unix (or Linux), if I want to run a shell script, I can start the file with #!/bin/sh. With awk, I start the executable file with #!/usr/bin/awk -f and it treats the rest of the file as the program.
How do I do that with a Java program? I tried copying the simple.class to simple, putting #!/export/appl/Mail/java/bin/java at the top and making the file executable, but I get:
69> ./simple
Error: Could not find or load main class ..simple
I know this can be done with an executable shell script, or a C program that execs the java interpreter. Every other interpreter on Unix can be called with a #! load card, surely there's a way to do it with Java.
The most usual way is to have a wrapper for the Java. A shell script that executes the "java -jar yourJar.jar" or equivalent. And then you bundle the shell script and the windows equivalent bat file with your product.
Another option is to have a native launcher. For example you can see the Eclipse project which has gone that way. You download Eclipse and you have a native executable to run. The native executable will launch your Java program.
One more option is to compile Java into native code. For example you can use this commercial tool called Excelsior JET ( http://www.excelsior-usa.com/jet.html ).
The Java class file format doesn't allow text before the header, that's why the Java runtime no longer accepts the .class file after your modification.
On Linux you can use binfmt_misc to support additional executable formats, including .class files. It's basically a way to tell the Linux kernel how to detect executable formats and how to execute them.
This Archilinux Wiki article explains in more detail how to get this effect.
You cannot do it with a Java program. Firstly, the Java program needs to be compiled before execution. Secondly, even if compilation wasn't required, the hash sign is not a comment in Java, so that would be a syntax error.
I've never heard the term "load card". What you have is an "interpreter directive" designated by a shebang. This merely designates which interpreter the shell should invoke on a given script.
As for why C programs can be run directly in the shell, executables recognized by the operating system are passed to the loader. A Java class isn't an executable, at least to the OS anyway. So the shell must know which interpreter to pass control to.
But as you've noticed, the shebang doesn't work. The reason is that the class file is in a specific binary format that the JVM expects. Editing this file will break convention and lead to an error. Therefore, there is no way to do what you've asked.
However, you can create a "shortcut" to the program you want to run by creating an alias or perhaps writing a one-line Shell script to wrap the java command you need. This is the common practice as I understand it.
The other answers explain why you can't do what you are trying to do. However, if your shell is zsh, you can create a suffix alias. For example, to execute .jar files using java:
alias -s .jar="/usr/bin/java -jar"
Then, to execute blarg.jar, you just type ./blarg.jar. Of course, you must chmod +x your .jar file first.
Apart from the wrapper script and binfmt_misc solutions suggested by others, I'd like to suggest a potential solution which doesn't directly answer your question but maybe it solves your actual problem.
Since Scala does have an interpreter that can run code without you having to compile it first, and this code can reference any Java code, if your goal can be summed up as "using Java as a shell scripting language", you could use a .scala file as your starter script (which can include the shebang to be run with scala) from which you can call all your Java classes. This isn't any simpler tha having a bash-based starter script, but it's a good starting point to gradually move to scripting in Scala instead of Java in which case you can get rid of the need to compile to .class file in the first place.
The reason this doesn't work is that Java isn't really an interpreted language, it's partially compiled, partially interpreted.
The .java source code that you'd put the hashbang directive in needs to be compiled to a .class file before the java interpreter can run it. Comments are stripped out by the compiler, so there's no way to push a comment from the .java into the .class file. The .class file is "compiled" output in a specific format, so adding a hashbang directive to the top of it would break the format.
Java isn't really meant to be a scripting language - but some JVM languages are. For example Groovy supports hashbang and so does Clojure.
so, I've been doing some searching, and i can find somethings on how to run an external application, but i cant get them to work! I've been working on this for a while, and its really annoying.
what i want to do is run a .jar file in the directory
C:\Program Files\AVTECH\NPS\Files\bin\NPS.jar
and I've tried a bunch of different things with the code
Process p = Runtime.getRuntime().exec("dir goes here");.
also
Process p = Runtime.getRuntime().exec("C:\\Program Files\\AVTECH\NPS\\Files\\bin\\NPS.jar");.
if i'm correct, it uses command prompt to do this? or at least the MS-DOS language. i did some of that kind of thing a few years ago, but i don't remember how one would do this... I've never worked with this kind of thing in java before...
could someone help please? thanks in advance.
Runtime.exec() is working just like if you were typing a command.
Launching a jar file is not working : you have to invoke
java -jar /path/to/my/jar
Check Oracle's documentation on how to execute a jar file.
The actual command should be java -jar C:\\Program Files\\AVTECH\NPS\\Files\\bin\\NPS.jar. I mean -- if the jar file is indeed executable, this doesn't mean it will run, by just trying to invoke it. You need to tell Java to run it as shown above.
In addition, MS-DOS is not a language -- it stands for Microsoft Disk Operating System. Nowadays, you have this as a Command-line Prompt (Shell) built into Windows.
You need to run the command as a call to the executable and a set of arguments. Check this version of Runtime.exec(String[] cmdarray). If need be, there's also a version of Runtime.exec() that takes a base directory in which to start the executable.
I love JOCL, Java bindings for OpenCL. I would like to run Cuda-memcheck on an executable from Java, but whenever I make Java applications, they are always just JAR files that point to a Main-Class. Is there a way to create a .exe file like C++ does and feed that to Cuda-memcheck?
This link could be useful: http://jsmooth.sourceforge.net/index.php
You could probably also the jvm executable directly and provide all the arguments necessary to run your java application. If you normally run your java applications through an IDE, check the console output to see what command the IDE uses to launch your app. This command should resemble something like: <path-to-java-exe> [JVM arguments] main_class [application arguments].
You might try looking into one of the Windows ports of the gcc toolchain. I know that gcc has the capability to turn java code into compiled binary, which is what Cuda-memcheck is looking for. If you aren't afraid of a lot of unnecessary output, attaching Cuda-memcheck to the call to the JVM should also work.
I am working on a .jar file library to implement a bunch of helper classes to interface a PC to a piece of external hardware. I'll also add some simple applications, either command-line or GUI, to handle common tasks using the library.
My question is, is there a recommended way to simplify the command-line instantiation of a JVM in a certain specific way?
e.g. instead of requiring a user to type a cryptic error-prone command like:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar arg1 arg2
instead I do
TurboBlenderApp arg1 arg2
?
I suppose I could use shell scripts (incl. win32 Batch Files... gacckkk), it's just that I'm not good at those + I was wondering if there was a more straightforward platform-independent way of running a Java app from the commandline.
When you use -jar, then-cp (and the CLASSPATH variable) will be ignored
Just provide a executable jar. "java -jar TheApp <whateverargumentsyouwant>" shouldn't be too hard (you can have a Class-Path attribute in your jar files manifest, however).
if it is too hard write a GUI
or provide those shell scripts/batch files. Writing those isn't too hard either.
If you just want to simplify local execution, use a batch file or even just create a custom shortcut. If you want to create a launcher and package the executable jar and required libs for deployment, use something like Launch4j.
For some reason this trick just doesn't get around. What you need to do is to create a custom manifest for your jar file that defines the Main-class: property, and include in the jar file all the class files you're using. Then all you need is to run
$ java -jar myapp.jar
On real operating systems, you can even just run the jar file, as they will use the magic number to start it. But if you must, a one-liner batch or shell-script containing that line is all that's needed.
This is described in one of the Java Tutorials.
Shell scripts and batch files are the standard way of accomplishing what you want. (Look at any major Java product.)
This is, of course, absolutely pathetic and ridiculous. Go Java.
Your alternative is to write a little C program that does what you need (creates the java process) and compile that for each of your supported platforms. (C is the truly platform independent language).
Either way, you will need to take platform-dependent steps to make your app feel truly at home in the OS. On OS X, you will need to make a .app bundle, on Windows you need to package atleast the icon and version info into an EXE. For Linux, well, shell scripts suffice. :-)
If you use java -jar to launch your application, you can add additional jars to the classpath by adding a Class-Path entry to the main jar's manifest. See the jar file spec for more information.
Another solution is found in the Jakarta Commons, as always : commons-launcher.
A batch file would do the job. Put the exact command you want executed into a file myProg.bat and run it.
It's pretty easy:
Write this in TurboBlenderApp.cmd:
java -cp TurboBlenderLib.jar -jar TurboBlenderApp.jar -DFoo=Bar %1 %2
// Bear in mind saua answer about -cp and -jar
Then from the command line you just write:
TurboBlenderApp arg1 arg2
Just the way you wanted.
If in Unix/Linux replace th4e %1 %2 with $1 $2 and make sure your app has execute rights ( If you don't know how to do this, I guess you don't need it either )