Execute batch file silent in background - java

Below are the batch file code.....
cd\
cd C:\Program Files\Project
"C:\Program Files\Project\jre\bin\javaw.exe" -classpath .;Project-jar-with-dependencies.jar;javafx-2.2.jar; com.ui.main.Main
Problem:
When i click on the batch file, CMD window open (black screen) but i want to execute that batch file silently in background means CMD window should not open.
A few suggestion are mentioned on Google but that execute the batch file using VB script but i do not want that solution.
Any suggestion. Thanks in advance.

Some simple answers that don't exactly "not open" but may be close enough.
create a shortcut to your batch file and in its Properties change Run to Minimized, and use it. The CMD window appears in the taskbar but not on the desktop.
since you only want to run one (nonconsole) executable, use 'start' to start it, then the batch file exits while the program (javaw) continues running. The CMD window flashes briefly and then disappears.
do both. The CMD window flashes briefy in the taskbar.
Also: If you are (or will be) using JRE 7 from Sun^WOracle, it now has javafx included and you don't need your own copy.

A .bat file needs cmd.exe in order to get processed. There is no way to hide its output window if you are executing it without an external host (like you've mentioned about VB). There would be different approaches to hide the window when it shows up, but this would require another application and be a very messy solution.

#echo off > nul
should do the trick (put it at the beginning of the batch file

Related

java export to jar with user input

I made a small program which takes a user input then get an output to a txt file.
The program runs without any problem in eclipse. Then i exported it as runnable jar file.
Now if I click the file, it just generates an empty txt file.
So is it possible to create a jar which takes user input? What did I do wrong?
Yes, that is possible. What's going wrong? Who knows, there's not much detail here.
Taking a wild stab in the dark:
You're on windows.
You're double clicking it to start the app.
You're asking for input via System.in.
On windows, an application has to declare whether they do command line stuff (show a black box) in the app itself. Therefore, only on windows, java.exe comes in two variants: java.exe and javaw.exe. The -w version is pre-declared to not take user input. The java.exe version will always show an ugly black box, even if the app takes no input or output. By default, javaw is used to open jar files, thus, you can't use System.in in that case.
There is no solution for the problem of 'make this app work if I double click it on windows' - in fact, there is no general solution to 'make it so that this jar file will run properly on any system I copy it to' - getting a functioning JVM is (since 10 years or so) no longer the responsibility of the user; it is your responsibility, you'd have to build an entire installer.
I presume you're still learning, in which case - hey, that's not the goal, right? Just.. open a dosbox (hit the windows key, type 'cmd', hit enter), and run your app as java -jar myjar.jar and all will be well. If you need to ship it, well, you can make a batch script that does that.
I assume this user input comes from the system input stream System.in.
It needs to run in a console/terminal/shell that provides an input stream.
For example, if you are running on linux, open a terminal and run this:
java -jar myprogram.jar
On Windows:
javaw -jar myprogram.jar

How to run jlink-generated Java runtime image without CMD window?

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.

How do I hide a console window that is running a java program?

I have a program that creates a batch file which in turn runs the program on startup. I want to hide the batch file so that all the user sees is the java program open, not the console window in the background.
EDIT: It seems that I'm not being clear. Here is the code in the batch file:
cd C:\Java\jre8
javaw -jar C:/Users/Me/Desktop/MyProgram.jar
I only need to hide the batch file.
javaw seems to be what you are looking for: http://docs.oracle.com/javase/6/docs/technotes/tools/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.
If you are looking to hide the windows .bat console, you can use vbs -- take a look at this page for various options for doing this: https://superuser.com/questions/62525/run-a-completly-hidden-batch-file .

Start a java program without the console

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

Is there a way to the hide win32 launch console from a Java program (if possible without JNI)

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

Categories