In a .bat file like this :
@echo off
start C:/my_test.exe
Why the first line is making no difference ? I thought with "@echo off", it won't open the command window, but it still flashed and then closed, it has the same effe开发者_开发知识库ct as without the first line. How to avoid the command window to open at all ? In other words, how to write the .bat file, so that when it runs, ONLY my exe file opens, the Windows command prompt window won't show up at all.
I want the app to run at start up, so I was told to create the bat file and put it in the Start up folder, so to not having the command prompt window show up at all, I was suggested to create a short cut, how to create a short cut and save it in the start up folder, I need to do it from my app, what does the DOS command look like ? It need to first find where the start up folder is, then create and save the shortcut in the start up folder.
@echo off just tell the shell interpeter not to print the executed commands on the command prompt. It does not mean the command prompt itself will not be executed, as the command propt (i.e. cmd.exe) is the environment where batch file run. In order to get the effect that you are after just create a shortcut to your exe.
Few of possible options:
Make installer for your application (most of them automate [startup]shortcut creation).
You can register your application in the registry for startup, here is sample command-line example:
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v Test /t REG_SZ /d test.exe
You can create shortcut with help of jscript or vbscript (examples).
Updated: if you don't want to go installer or jni/jna solutions, here are q&d examples for [2]:
String command = "reg add \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run\" /v Test /t REG_SZ /d test.exe /f";
try {
int result = Runtime.getRuntime().exec(command).waitFor();
System.out.println("command execution " + ((result == 0) ? "succeded" : "failed"));
} catch (Exception e) {
// handle error
e.printStackTrace();
}
and [3]:
// construct your script body
String script = "Set Shell = CreateObject(\"WScript.Shell\")\r\n"
+ "Folder = Shell.SpecialFolders(\"Startup\")\r\n"
+ "Set link = Shell.CreateShortcut(Folder + \"\\\\test.lnk\")\r\n"
+ "link.Arguments = \"arg1 arg2\"\r\n"
+ "link.Description = \"Test shortcut\"\r\n"
+ "link.TargetPath = \"c:\\\\bin\\\\test.exe\"\r\n"
+ "link.WorkingDirectory = \"c:\\\\bin\"\r\n"
+ "link.Save\r\n";
File vbs = new File("test.vbs");
FileWriter writer = null;
try {
// write script contents to file
writer = new FileWriter(vbs);
writer.write(script);
writer.close();
writer = null;
// execute
Runtime.getRuntime().exec("cscript.exe \"" + vbs.getAbsolutePath() + "\"").waitFor();
} catch (Exception e) {
// handle error
e.printStackTrace();
}
if (writer != null) { try { writer.close(); } catch (Exception e) {} }
The console window is opened before your batch file starts running. Therefore, there's nothing you can put inside the batch file to prevent the console window from appearing.
However, you can control how the console window is launched. If this is from a shortcut on your desktop, there should be a setting in the shortcut that tells the window to open minimised, for example.
If your batch file really is as described and only launches one executable, why not simply launch the executable directly instead of going through a one-line batch file?
hi this might be a simpler solution http://www.portablefreeware.com/?id=1660 is a Bat_To_Exe_Converter.exe it will convert your batch file to an executable with an option to run silently (without seeing the CMD window) i love this little tool.
精彩评论