I have a Java application and i would like to spawn a new process(start a .bat file), that will essintially do the same thing as by double-clicking on it.
I have tried both Runtime.getRuntime().exec() and ProcessBuilder in order to spawn that process. Both those approaches work (they can start the .bat file), but my problem is that they do not actually do the exact same thing as by double clicking on it.
More specifically, this .bat file starts up a JVM (java.exe MyMainClass) which is configured to run using Windows SxS (side by side). Thus, i have created appropriate java.exe.config and java.exe.manifest files. When i doulbe click on that, the java application starts and the appropriate .dlls are loaded succesfuly (reason i need SxS).
My problem is that when i start the exact same .bat file (with the exact same arguments and process environment), either by using ProcessBuilder or Runtime.getRuntime().exec(), it doesn't seem to take into consideration my SxS configuration, thus the .dlls that i need are not loaded at all, resulting in errors.
Does anyone have any clue how to launch this .bat file the same way as windows laucnhes it when i am double clicking on it?
Additionally, does anyone have any experience with Java SxS deployment? I cannot really understand why ProcessBuilder ignores my SxS configuration.
Thanks in advance.
You can try starting a cmd window, which loads your application bat file:
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"C:\\path\\to\\the\\app.bat arg1 arg2\"");
Related
Everybody probably has used install .exe files. But how to make them and when does it make sense to make one?
For example, I would have had programmed commercial software in Python, c++, etc. with different files a GUI and pictures and all of the other stuff.
When I want to deliver my product to my customer I don't want to give them a folder and say you need to install Python or Java and execute the program via your command line.
How can I create an executable file that installs the required language and sets up local instances and arranges all files into the correct order?
To create an executable file from a python program, there is pyinstaller. I don't know about java at the moment. The command is as follows :
pyinstaller fileName.py
You can add args (and there are 2 really helpful ones) :
pyinstaller --onefile -w fileName.py
--onefile will put everything into one single file (recommended) and -w will prevent the console from opening when running the .exe file. Add it if you're running a GUI or something. If you need to console for input, don't add -w.
If you want to automate a command line in cmd, create a shortcut leading to C:\Windows\system32\cmd.exe and add /k and your command. For example :
C:\Windows\system32\cmd.exe /k ipconfig
Double clicking on the shortcut will now run the cmd and execute ipconfig automatically. If you want more than one command, you can do command_1 && command_2 && ... && command_n
There is a setup program built into Windows.
Type
iexpress
and follow the wizard.
You need to provide code to run when it finishes to actually install the extracted files.;
I've created Java runtime image for a simple OpenJFX application. In order to run this app, jlink auto-generated two lauch scripts under %image_path%/bin directory. This how it looks like (the one for Windows):
#echo off
set JLINK_VM_OPTIONS=
set DIR=%~dp0
"%DIR%\java" %JLINK_VM_OPTIONS% -m app/com.package.Launcher %*
Obviously, when I run this batch file it opens new shell window, which is not what I want to. I've tried all common approaches: use javaw instead of java, run script via start command etc. Nothing works.
Is it possible to avoid shell window or somehow create native launcher?
Ok, I've figured out it's not posiible to eliminite shell window completely. In the best scenario it's just flickers for ~1sec. This is how it can be achieved:
#echo off
set JLINK_VM_OPTIONS=
set DIR=%~dp0
start "" "%DIR%\javaw" %JLINK_VM_OPTIONS% -m app/com.package.Launcher %* && exit 0
There is a feature request about native laucher implementation but it's not discussed actively.
Nonetheless I've solved the problem. There is "Batch to EXE Converter" tool. It can generate executable (basically the same batch file) which can run your app silently.
This looks to be possible using vbscript. If you put the following script into a .vbs file next to the launcher.bat file (or whatever the name of the batch file is):
CreateObject("Wscript.Shell").Run CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\launcher.bat " & WScript.Arguments(0) & " > " & CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName) & "\launch-log.log", 0, False
This runs the batch file in the same directory, and also redirects stdout to a log file.
What you´d like to achieve is very well possible. It is actually even quite easy and I use this every day. There already is an early access build of jpackage available here: http://jdk.java.net/jpackage/ Creating executables works already
nicely (I use it on Mac and Windows). Only creating installers is still a bit problematic.
It's very easy to run a bat file without showing the cmd window.
you just need to create VBS file to run bat file with the following cmd
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & ".\bin\launcher.bat" & Chr(34), 0
Set WshShell = Nothing
Save cmd in the file out of the bin folder with any name like
Launcher.vbs.
TL;DR: First java application launches second java application, but only after exiting itself.
I've got two executable jar files (exported from eclipse). I'm starting the first one through command prompt with the "java -jar first.jar" command. Part of what this jar does is starting the second jar. I've tried both of the following:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "Second.jar");
Process p = pb.start();
and
Runtime.getRuntime().exec("java -jar Second.jar");
Now, when I run a small test jar as second.jar, there's no issue. However, for my purposes the second jar is substantially larger (using JavaFX, JDBC connectors, JXBrowser components and launching an external process itself). When I replace the simpler second.jar with this one, something odd happens. For all intents and purposes nothing seems to happen, but only as long as first.jar is running. As soon as it's done its work and exited, or if its process is killed, second.jar launches perfectly. I'm not sure what's doing this so I don't know what to try. I'm using log4j2 and according to the logs, the entry into the main method of second.jar doesn't happen until after the first.jar has closed.
TL;DR
I am trying to use this GUI mod for a Minecraft Server. I wrote a batch file so the server can start with more RAM. When I run just the .jar file, no command window opens and it runs just fine (of course with about 256mb ram) I was reading online that javaw starts a jar file without a command-line-console. But when I use javaw, the command console opens, but when I close it the program remains open. this is my batch file:
#echo off
"%ProgramFiles(x86)%\Java\jre6\bin\javaw.exe" -jar -Xms1024m -Xmx1024m crafty.jar
#echo on
I don't understand java as well as most, so please try to be as clear as possible. Thanks
If you want to start a java program without console popup under windows, this should be helpful:
In command prompt type the following:
start javaw -jar -Xms1024m -Xmx1024m crafty.jar
If you want you can also write this as a batch file.
You should Create Shortcut of "%ProgramFiles(x86)%\Java\jre6\bin\javaw.exe", let's name it as Minecraft, then
edit the Properties of Minecraft shortcut. In the Target textbox, append -jar -Xms1024m -Xmx1024m crafty.jar in the end of javaw.exe
change the Start in as the folder which contains the crafty.jar
Double-click the Minecraft icon to star the server.
That's all.
Create a .bat file with
start javaw -jar yourjar.jar arg0 arg1
start javaw -jar yourjar.jar arg0 arg1
will open the console, but close immediately. it is different from running window .exe.
You will always get the command window opening and closing because you are starting it inside a command window or batch script (which launches an implicit command window to run itself).
In order not to get a command window you must open the file from "not a command window" i.e. an executable launcher.
Take a look at Launch4j which can run a java program from an exe. It can also hide-away the jar file inside the exe if you like.
http://launch4j.sourceforge.net/
There's a little YouTube clip showing them creating an exe from a jar.
A batch file is a way of starting the command prompt with the code pre-written, using javaw is a way of open then closing the prompt. Like I said a batch is a commands prompt you can't stop it from opening.
It's few years, but for windows today as for linux you have the supervisor (pytho based)
supervisor windows based on python
You launch a java program from a console (maybe using a .bat script).
I don't want the console to remain visible, I want to hide it.
Is there a simple way to do this ? Without JNI ?
Use javaw.
http://java.sun.com/javase/6/docs/tooldocs/windows/java.html
The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.
You can start a java application with start javaw. It will hide the black console window.
This .bat trick works for general programs so I think it should also work for launching java program:
Call start program instead of just program in your .bat script
You can hide the console by using javaw.exe (java without) instead of using java.exe.
One of the most useful associations to set up is to make *.jar files executable with java.exe. Then you can just type the name of the jar on the command line to start it executing.
If you use javaw.exe rather than java.exe you won’t see the console output. Watch out, Java installers often associate *.jar files with javaw.exe instead of java.exe, overriding your setting.
download jsmooth and create your own custom exe in a minute or two. Then just use that exe to launch your java app. You can even get slick and bundle a JRE with your app.
http://jsmooth.sourceforge.net
In case fo running from but file your script should look like
start javaw start javaw -jar ***.jar
Note, that you may need running javaw.exe by providing full path to the file, that may need adding quotes " in case there are spaces in the path. The quotes will trigger recognition of them as "title"-argument for the "start" command.
So, use following correct format:
start "MyTitle" "c:\Program Files (x86)\Java\jdk1.8.0_202\bin\javaw.exe" -jar myApp.jar
where title can be empty if needed