How to automatically let cmd close Batch File [duplicate] - java

This question already has answers here:
Difference between java.exe and javaw.exe
(4 answers)
Closed 1 year ago.
So i have a Javafx Maven Projekt which i run with a .bat
#echo off
set JLINK_VM_OPTIONS=
set DIR=%~dp0
"%DIR%\java" %JLINK_VM_OPTIONS% -m Main/org.openjfx.Main %*
it works but the cmd stays opend and when i close it the project closes aswell
i asked the question here but i didnt got a working answer so i just ask here
btw sorry for my bad english

You seem to be uncertain what you need to be kept open, (the sole aim of that batch file is to keep a console window open for visual console feedback) but to answer your request, you need to run your bat file from a cmd prompt, in order to pass parameters otherwise it is mainly redundant.
#echo off
set "JLINK_VM_OPTIONS="
set "DIR=%~dp0"
start "" "%DIR%javaw" %JLINK_VM_OPTIONS% -m Main/org.openjfx.Main %*
rem See notes
rem exit
Note
javaW willstart java for windows in non console mode, thus dismissing the batch file. However if you are running from a cmd console that is a separate exit that's needed as an extra last line, so try with rem first then remove that last rem to see any difference.
If you find javaW is not suitable then remove the W at the end.
I have no idea why the source of that file was constructed in such a non windows fashion except the aim seems to be to prefix the run by clearing one and setting one preset environment value, then holding so could be reduced to
#Title "Feedback"&set "JLINK_VM_OPTIONS="&set "DIR=%~dp0"&"%DIR%java" -m Main/org.openjfx.Main %*
#echo Done&pause&exit

What i understand from you is like this question How to automatically close batch program, but keep java program running?
try this if it works.

Related

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.

java launch no console window in Windows 7

I've already seen this questions, but have no positive result:
1) Is there a way to the hide win32 launch console from a Java program (if possible without JNI)
2) Start a java program without the console
I've something like that:
"C:\Program Files (x86)\Java\jre1.8.0_71\bin\javaw" -Dfile.encoding=UTF-8 -cp ".;manyjarshere.jar" main.Main
And console appears, but I can close it manually, but I want it hidden by start.
If I place "start" before "C:..." then it shows "not found "-Dfile..."
start javaw [args] should do the trick IF java /bin folder is added to the system's PATH environment variable

How to launch programmers notepad from command prompt

ok in cmd prompt when i give a this command notepad xyz
it launches the notepad application . but instead of notepad i want it to launch programmer note pad since im going to use this mainly for practicing /learning java.
I set the path variable for the programmers notepad directory that didn't help at all.
Have you tried reopening the terminal after updating the path env variable? Also first try to see if it works by typing the full path yourself. For example: I have a file.xml inside c:\. I typed "C:\Program Files (x86)\Programmer's Notepad\pn.exe" file.xml in the terminal and it works as expected.
I then added C:\Program Files (x86)\Programmer's Notepad\ to the path env variable that is included in the System Variables section. Closed the already opened terminal, reopened and then typed pn file.xml (with c:\ being my current directory) and it works again as expected.
First, check properly whether you have added the PATH variable correctly!
And then Run : pn.exe in command prompt (cmd.exe), not notepad....

how to leave cmd open after batch file executes

I am current'y taking a Java course and because of security reasons it won't let us set environment variables for the Java compiler. What we have to do all the time is open cmd and then put
set path="path_to_java"
This gets really annoying because when we close the command line it loses the path. I was able to create part of the .bat file but when I execute it, then it closes instantly. I know you are able to put PAUSE but then it won't let us insert any Java code.
Is there any way to create a .bat file and fix this problem so when I double click it creates path variable and it lets us compile Java code?
I currently only have this
SET PATH "path_to_compiler"
CLS
There are several things you could do. One way would be to create your batch file somewhere (e.g. in your profile folder) and make it automatically execute whenever you start a command prompt:
reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "%USERPROFILE%\init.cmd"
However, since you're not allowed to set persistent environment variables, this may be prohibited as well. In that case you could create a shortcut to your batch script on the desktop, then open its properties and change the target to something like this:
%COMSPEC% /k C:\path\to\your.cmd
%COMSPEC% is the CMD executable, and the option /k prevents it from automatically closing after the script finished.
As a side note, you may want to include the current %PATH% with the path to the compiler, otherwise stuff may stop working (e.g. because some command line utilities can't be found anymore):
set PATH=%PATH%;C:\javac\folder
I haven't checked for duplicates, but I'm pretty sure I've seen a question about this recently. Anyhow, you aroused my curiosity as to how to do this, so I experimented, and fortunately the first thing I tried worked.
So, basically what you should have is...
#echo off
::Add your code under here
set path=path_to_compiler
cls
call cmd
One thing I must ask, is it absolutely necessary to make the variable called "path" instead of something else? I'm asking this because path is an important variable that the interpreter uses to do things.
Anyway, this basically just runs your code then opens up cmd.exe within the current window.

I Cant get a .jar file to run after being double clicked on windows 7

i have tried several things so far, i have used a .bat file to run it, and it opens up the command prompt, but closes right after opening. i have a manifest.txt file already created along with the .jar. If i type -java -jar DesktopApplicationRunner.jar in a command windows that is already up it will run the program.
my .bat currently says:
-java -jar DesktopApplicationRunner.jar
Apologies just read your comment saying you have inputs. your problem is using the bat file, unless you have user inputs, the application will start, open the command line, then close when finished, the best way is to manually type in the command line, so it doesn't close the view down afterwards, sorry if this makes no sense.
-Ian

Categories