This question already has answers here:
How to call java from C++
(4 answers)
Closed 8 years ago.
I wrote a code in java that adjust the system sound volume based on value supplied as command line argument. I would like to execute this code from my C++ application. What is the best way to do this...
Do I need to install the jdk on the deployment machine?
You can bundle a runtime with your software and then call this from within your program, however, the better solution would be to perform the function that the Java app does natively within your C++ application.
You can run your Java application from C++ as you can run any other executable.
Run "java.exe" with your Java class name as a parameter, as you would do normally when executing Java applications from the command line:
java.exe MyClass myParamToJavaClass
Alternatively you can pack your class in an executable jar archive, in which case your command to run your Java application would look something like this
java.exe -jar MyJarr.jar myParamToJavaClass
Substitute the ".exe" by something else if you aren't running under Windows.
In the examples above i assume that "java.exe" is in a directory defined in your "path" environment variable, as it normally is.
You do not need to install JDK but a JRE needs to be present on the deployment machine.
Related
From a Java application I want to run another Java application on the same Java installation but in a separate process.
AFAIK for a new process I would use ProcessBuilder to run a command like
java -jar my.jar
but what if java is in a different directory, or should be java.exe since we are on Windows, or java.exe has some other name since the first application was jlinked and jpackaged?
Edit: What I learned meanwhile is that a jpackaged application comes with a native executable that sits in front of the JVM but passes all arguments to the application. That means it is no longer possible to specify an alternative jar to be executed, and some other mechanism is necessary.
If jlink image used within jpackage based apps is built without using the --strip-native-commands flag then the runtime image will contain bin/java (or bin/java.exe and bin/javaw.exe on Windows).
This will mean that you should be able to determine a path to launch a new JVM which works inside or outside of jpackage apps just based on the current runtime's java.home and by appending extra VM arguments in normal way for the classpath and main / module of the other application.
On Windows the environment property echo %PATHEXT% usually contains .EXE so the following should specify the java executable for the current runtime without needing to add the correct file extension on Windows:
String java = Path.of(System.getProperty("java.home"),"bin", "java").toString();
You can test above inside your jpackage and non-jpackaged app with a simple one-liner (note that this is not proper way to use ProcessBuilder):
new ProcessBuilder(java, "-version").start().getErrorStream().transferTo(System.out);
Obviously, the above is no help if you wish to determine whether to use Windows console enabled java.exe versus non-console javaw.exe.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a java program compiled with jdk13, and installed jdk13 on the server running it. If I run the program from the commandline, there are no problems, however when I call it from my python script with subprocess.Popen, I get this error message in stderr
has been compiled by a more recent version of the Java Runtime (class file version 57.0), this version of the Java Runtime only recognizes class file versions up to 55.0
How can this happen through the python script and not when I run it manually in the commandline?
Your python script might be using a different environment where java means something different (id est, points to a different executable).
Try call directly you can find in your command line with which java.
How can this happen through the python script and not when I run it manually in the commandline?
Basically, the version of java that is used with depend on:
the full pathname that you use; e.g. /usr/bin/java or
the PATH environment variable if you are launching it with a simple command name; e.g. java.
We can't see what your python script is doing, but there are a number of ways that you could get different behavior.
The script could be using a different full pathname for java.
Python or your python script could be overriding PATH.
You could be launching your python script via a shell script that is setting PATH differently. Note that shell initialization is rather complicated, and typically it sets up PATH depending on what init files get run.
If you are using sudo, that overrides PATH.
The PATH setting in your command prompt could have been set "by hand".
And that is just some of the more likely possibilities ...
This question already has answers here:
Determining location of JVM executable during runtime
(5 answers)
Closed 2 years ago.
I have some Java code that launches a new Java process, effectively using the default system JRE (in my case JDK 8). Instead I need it to run with the version that is running the original process (e.g. JDK 9).
How can I do that? (The solution needs to work on both Java 8 and 9.)
Details
The code currently relies on the default system JRE by simply issuing a java ... command. An alternative would be to use something akin to System.getProperty("java.home") + "/bin/java" (but platform independent), but both have the same problem (for me): They launch a Java process with the JVM version known to the system.
UPDATE: That's utterly wrong, System.getProperty("java.home") indeed does what I want and returns the current JVM's home directory. (Stupid me, I thought I tried that.)
One way to launch with the same Java version, would be to ask the current process for it's executable and reuse that one, but I found no API for that.
Solution for Java 9 only. With
ProcessHandle.current().info().command().map(Paths::get).orElseThrow();
you get a handle to the current java executable: <JAVA_HOME>/bin/java[.exe]
Now, use that (absolute) path with the new Process API to your liking.
If you cannot use the new Java 9 API yet, here you go:
static String getJavaRuntime() throws IOException {
String os = System.getProperty("os.name");
String java = System.getProperty("java.home") + File.separator + "bin" + File.separator +
(os != null && os.toLowerCase(Locale.ENGLISH).startsWith("windows") ? "java.exe" : "java");
if (!new File(java).isFile()) {
throw new IOException("Unable to find suitable java runtime at "+java);
}
return java;
}
For more details you can take a look how we do it in JOSM's restart action, which has the following requirements:
work on Java 8+, Linux, Windows and macOS
work with java running a jar file or class files directly (e.g. from an IDE)
work with Java Web Start and macOS packaged applications
work with paths containing space characters
The code proceeds as follows:
finds the java runtime using java.home system property
checks whether the sun.java.command system property is available (not the case for IBM JVM), then extracts program main class and program arguments from it.
reconstructs the command line either by adding -cp or -jar arguments, depending on what we found at previous step
runs the command using Runtime.getRuntime().exec
There is also a lot of stuff concerning JNLP (WebStart) and macOS applications. I assume you're not interested in it but I can explain it if you want.
so apparently if you create an executable jar, in order to run it you still need the java command:
java -jar something.jar
but what if I just want it to run without the java command, so just directly from the command line
something.jar
is there a way to export my java app in eclipse in order to accomplish such
On Unix systems you can append the jar file at the end of an executable script.
On Windows you have to create a batch file.
For instance in Unix:
$cat HelloWorld.java
public class HelloWorld {
public static void main( String ... args ) {
System.out.println("Hola mundo!");
}
}
$cat M.mf
Main-Class: HelloWorld
$cat hello
#!/bin/sh
exec java -jar $0 "$#"
$javac HelloWorld.java
$jar -cmf M.mf hello.jar HelloWorld.class
$cat hello.jar >> hello
$chmod +x hello
$./hello
Hola mundo!
In windows you have to create a batch file like:
::hello.cmd
javaw -jar hello.jar
Which has the same effect.
On Windows and OSX you can double click on the jar to run it, I'm pretty sure you may add a trigger on Linux too.
I hope this help
Excelsior JET - http://www.excelsior-usa.com/jet.html - claims to compile to native code and bring its own runtime support, so it does not require an existing JVM. Commercial product.
I have not tried it myself, but they have spent quite a bit of effort over the years to market JET as a great deployment method for precompiled binaries.
Also note that if you have an executable/runnable jar which works fine with "java -jar someting.jar" and you just want to be able to invoke it in a more convenient way, this is the job of the program accepting your command and launching the java command.
For Linux you can frequently add an alias saying that "something" expands to "java -jar something.jar", and some command interpreters allow for saying that all commands ending with jars should be executed specially. The exact details depend on which shell (command line interpreter) you are using.
What you need is a tool called 'Java Executable Wrapper'.You can use it to Pack all your class files to a Single Executable Package.
The One i recomment is launch4j,you can download it from sourceforge launch4j.sourceforge.net
Launch4J can be used to create standalone Executables (.exe) from a jar file for windows Environment.
The thing is, that Java gets interpreted by the JVM, so you'll at least need to ship it with your app.
To be a little more specific about this, Java gets kind of compiled to byte-code so it can be interpreted faster. But the Byte-Code can't run without the JVM. This is the nice side of Java: You don't need to recompile your Apps to run on other platforms like Linux or OS X, the JVM takes care of that (as it is written in native code and is recompiled for those platforms).
There are some compilers out there which can convert your Java code to something native like C which can then be executed without the JVM. But this isn't the idea behind Java and most of those tools suck at what they do.
If you want your App to run without any interpreter, you'll need to use a compiled language like C or C++
Java program runs on a JVM, for the first question I don't think there's a compiler that can do the job well. For the second question since a jar file is not an executable per se, there must be some sort of settings in the target machine, "executing" a jar file without providing the java command is a matter of convenience for the user. On Windows every file extension has a program associated with it, so .doc documents have (usually) Word as the program associated -that setting is set by the office installer, the java runtime also sets the setting for .jar files when you install it, but behind the scenes, java command will be used by the system. So the short answer to the second question is: depends on the target machine.
I have done image processing in MATLAB and build my GUI in Java. I want to integrate MATLAB into Java. I want to use MATLAB Builder for this purpose. I want also to use neural network for classification. There are some excel files also. Is it possible that this code will be integrated in Java?
My other question is that I want used MATLAB BuilderJA to know how it works. When I type java -version command, it gave me this error.
??? Attempt to execute SCRIPT java as a function:
C:\Program Files\MATLAB\R2009b\toolbox\matlab\general\java.m
C:\Program Files\Java\jdk1.6.0_21
When I use build command it gave me this error.
'javac' is not recognized as an internal or external command,
operable program or batch file.
Error: An error occurred while shelling out to javac (error code = 1).
Unable to build executable.
I have JDK installed. The path is C:\Program Files\Java\jdk1.6.0_21. I am using R2009b version
I want to run my code in Java, but I do not know how to fix this error. Can any one tell me how to resolve this error?
The first error message which you get when you type java -version is a bit misleading; instead of
java -version
you need to say
!java -version
since you want to call an external program and not a MATLAB script or function. As stated in the comments by Amro this will only work if the directory containing java.exe is on your path. See Running External Programs in the MATLAB help for more info.
The error message you get comes from the fact that
there happens to be a file java.m and MATLAB thinks you are trying to call this file
that file only contains comments, since java is actually a sort of keyword in MATLAB, see doc java.
MATLAB realizes that you are not using the keyword in its correct form (which would be to call java.something to create an object of class something) since you give a parameter
MATLAB ends up telling you in a strange way that java doesn't accept parameters (even though java.m does not contain the script, only its documentation)
Note that if you don't want to add the directory containing java.exe and javac.exe to the path, you could also try calling them with their full path name:
!C:\Program Files\Java\jdk1.6.0_21\blablabla\bin\javac.exe