I am using a jar to run some code as a helper for a program (OSX). I want to open this jar programmatically, and have been using a ProcessBuilder to run it through the terminal.
However, I want to give the jar some arguments (specifically a file location, but that's irrelevant). I have using java -jar jarName arg, but this doesn't work with people who don't have Java tools installed.
I have tried to use open jarName --args arg, but the jar doesn't recognize the args.
As a test, I am just using the following code for now.
public static void main(String[] args) {
try {
// set a PrintStream to see the args presented
System.setOut(
new PrintStream(new FileOutputStream(new File(System.getProperty("user.home") + "/Desktop/argsTest.txt"))));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(argsSize: "+args.length");
for (String s : args)
System.out.println(s);
}
I am fine with trying other methods of opening jars, so long as they are available on all up to date systems.
I have the JRE packaged in the application, is there a way to use that?
Bundle jre with with your program. Refer to Bundle JRE along with executable jar
or if you are using netbeans - it will allow you to test it first. https://netbeans.org/kb/docs/java/native_pkg.html
how to run - https://netbeans.org/kb/docs/java/native_pkg.html#check
Welcome on StackOverFlow!
Sadly, the answer is no, you can't do this. If you want to run some java code, from a jar or not, you will need to have java installed on the machine. Specifically, you need the JRE to have the JVM.
But you can provide java by your own when deploying your app. See this threads:
https://superuser.com/questions/745112/how-do-i-run-a-jar-file-without-installing-java
Running java without installing jre?
for more informations.
Related
I'm getting started with java development. So, I installed JRE and JDK in my computer.
Then, I created a simple Example.java file and saved it in my Desktop.
In the prompt, I executed
javac Example.java
and it worked ok. A .class file has been created in my Desktop.
Then, I tried to run the class, executing this:
java Example
and I got an error in a window alert, with this message:
Java Installation Not Completed
Unable to install Java
There are error in the command line switches: "Example";.
Check that the commands are valid and try again
Then, for testing, I executed both commands:
javac -version and
java -version. Both are installed in my computer.
What am I doing wrong?
I am running Windows 8 and have already set my environment variables.
Example.java:
public class Example {
public static void main(String args[]) {
System.out.println("Finally Java");
}
}
Try to remove installation again, look for all leftovers and remove them manually, if you changed the directory you installed java to, remove environment variables as well and set them again. You should also make registry cleanup: https://java.com/en/download/help/manual_regedit.xml Make installation through offline installer.
My client appears to have Java Runtime Environment loaded on her computer (both x86 and 64 bit versions), but desktop Java programs won't run.
I wrote a simple program to test the Java version and make sure it was functioning properly, but even this little program won't run. (It runs fine on my computers.)
import javax.swing.JOptionPane;
import com.sun.servicetag.SystemEnvironment;
public class VersionTesterWithBits
{
public VersionTesterWithBits()
{
}
public static void main(String args[])
{
JOptionPane.showMessageDialog(null, "Java Version = " + System.getProperty("java.version")
+ "\nBits = " + System.getProperty("sun.arch.data.model"), "Test", 1);
System.exit(0);
}
}
I also tried running it using a batch file (similar to below) with each Java file (x86 and 64-bit) to see if one of them was the problem. Neither worked.
SET PATH=C:\Program Files (x86)\Java\jre7\bin
java -jar VersionTesterWithBits.jar
I also tried running java -version from the command prompt and nothing came up. (i.e. "java is not recognized as an internal or external command, operable program or batch file")
They're supposedly running Java 7 Version 40. It looks like the desktop version, but is it possible this is a web-version? (Or would that be in a browser directory?)
Any thoughts on what the issue could be?
Check the PATH, and make sure that java's bin folder is on it. Sometimes when you do an update of Java, it doesn't update the path.
Edit- I see that you're setting the path variable, but make sure that actually worked. Is that actually where Java is located? Shouldn't that be in quotation marks because of the spaces?
Apparently the problem was unrelated to Java. There was an issue with her Windows profile.
Just today I noticed that I can run java in eclipse with no problems but when I try to run it in the command prompt, I get "cannot find or load main class." The command prompt actually compiles all right, and it outputs a .class file, but then it displays the error msg when trying to execute. (Also, I was able to run java in the cmd a couple weeks ago.)
/* work area for practice
*
*/
package Scrap;
public class experimentational {
public static void main (String [] args) {
System.out.println("welcome to java!");
}
}
Found the answer: (i'm using different code but it is still relevant to this problem)
java -cp . hiThere
output: "Hi there"
I know this is classpath but don't know why it works or what the period does for it. Anyone have an idea?
Use:
javac Scrap/experimentational.java
followed by:
java Scrap.experimentational
try java -cp . [your main class].
Did you install a JDK on the machine outside of Eclipse? If you did, then make sure you set your path variables correctly. Open a command prompt (assuming windows) and type java -version
If the JDK was installed properly and path variables were set properly it should tell you the version of Java that was installed. If it tells you that 'java' is not recognized as a command that you do not have a JDK installed, or it was not installed properly.
The reason your program runs in Eclipse is that Eclipse for Java has its own internal JDK and JVM.
Your other option is to set up your path variables to point to Eclispe's internal JDK.
If you were able to run it from a command prompt previously then most likely your class path was altered. Is this a machine at work? Some companies have SMS tasks that come through periodically and restore default system settings (including path variables) to corporate defaults.
Maybe java and javac isn't in your OS path.
If you are using Microsoft Windows in cmd type path and then enter.
If jdk or jre isn't in path you need to put them to it
I had a similar issue when I copy pasted code into an editor. I removed the package declaration on line 1 and it ran then. So I'd investigate above comments on packages, after trying first to remove the package line.
I'm making an editor-like program. If the user chooses File->Open in the main window I want to start a new copy of the editor process with the chosen filename as an argument. However, for that I need to know what command was used to start the first process:
java -jar myapp.jar blabalsomearguments // --- need this information
> Open File (fileUrl)
> exec("java -jar myapp.jar blabalsomearguments fileUrl");
I'm not looking for an in-process solution, I've already implemented that. I'd like to have the benefits that seperate processes bring.
Since you are launching Java -> Java, you can use the existing classpath to set the classpath on the command line. This type of thing works really nice in the dev environment too.
ProcessBuilder selfLauncher = new ProcessBuilder(
"java", "-cp", System.getProperty("java.class.path"),
"com.my.mainClass" );
selfLauncher.start();
Update:
For executable jar files, you will have a classpath which is simply the relative path to the jar file itself. If you want the command line arguments, you will have to save them from main, and re-apply them when launching.
You can see this by packing the following program into a jar. I'm not actually sure what happens if you have jars inside the executable jar file. They probably show up in the classpath.
public class TestJarPath {
public static void main(String args[]) throws Exception {
for (String s : args)
System.out.print("[" + s + "] ");
System.out.println();
String cp = System.getProperty("java.class.path");
for (String s : cp.split(";"))
System.out.println(s);
}
}
For java -jar ..\tst.jar X, you get output like:
[X]
..\tst.jar
If all else fails, try writing a batch/shell script to launch your app. In windows you can pass %CmdCmdLine% to Java to get the entire command line.
See http://www.robvanderwoude.com/parameters.php
As far as I know is there no portable way to get this info. I found a property in the gcj runtime but I doubt this will cover a large percentage of the users.
I think the accepted practice is "Try and Pray" :
Hope it is on the path, (the path IS available, so that can be checked)
if not, check if JAVA_HOME is defined, and use that to find java.
if not check in the most likely places on all OS's you have received bug reports for.
Well, it is messy... porbably best to check for JAVA_HOME and the path and ask the user to configure a JVL explicitely if that fails.
Synopsis: When calling an executable that links to shared libraries from Java code on Tomcat, I get errors on Windows 2003 whereas the executable works great on a command prompt.
I wanted to do this in Linux, but, alas, got outvoted and have to implement it using Windows Server 2003. Here it goes,
I have a simple Java code running on Tomcat which, when it receives a "start" signal from another server has to run an external executable (written in C++, using shared library DLLs from OpenCV and ffmpeg) like so
String cmd = "c:\\workspace\\process_video.exe -video " + filename;
// Execute the command
Process proc = null;
try {
proc = rt.exec(cmd);
} catch (Exception e) {
System.out.println("VA-> Exception thrown in running the command!");
errorOut.append(e.getStackTrace().toString());
}
Now, when I run the command in process_video from a DOS command prompt, it works (doesn't matter which directory it's issued from). However, when it is run through the Tomcat->my Java code->rt.exec() chain, cmd doesn't get executed, although the exception doesn't get thrown. When I examine Windows event logs, I see an APPCHRASH event for process_video with Fault Module Name cv110.dll, which is one of the OpenCV DLLs I link from cmd.
One solution would be to stuff all the DLLs used in process_video into the tomcat\lib directory, but this hurts my programmatic sensibilities, so I want to know if there is a better way to solve this issue. What user does Tomcat use when running executables on Windows? Maybe I can give more privileges to that user? Should I add the DLL paths to Tomcat's configuration file?
Any help will be much appreciated,
Thanks!
Cuneyt
Add an entry in the PATH evironment variable that points to where your DLLs are. If this doesn't work for your app, you can try adding the entry to Tomcat's PATH. You have to modify the PATH variable of the process that will be loading the executable. Since your Java code probably shares a JVM (and hence a process) with the Tomcat executable, that will dictate which environment the PATH variable will need to be updated.
This is a Windows problem, not a Tomcat problem.
By default, Windows looks in %Path% for DLLs, which may not include the directory of the EXE file.
You can fix this by creating an empty file called process_video.exe.local in the same direcotry as the EXE ( i.e. c:\workspace )
You can also create a .manifest file, but this is a bit more complicated.