So i have a java project made in eclipse with sphinx voice recognition. If i say a certain word then it runs a .bat file.
if (resultText.equals("word")) {
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("C:/c.bat");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
In Eclipse it works fine, but after i export the .jar and run it, if i say that specific word, it doesn`t run that .bat. So any ideas why this only runs my .bat file from eclipse and not from command line? Thanks
I am not sure about this but atleast try this solution once.
Try giving the .bat file path as C:\\c.bat and then try again.
Try adding something like:
File f = new File("c:/c.bat");
if(f.exists()) {
// execute the file
Process process = runtime.exec(f.getAbsolutePath());
process.waitFor();
InputStream stdout = process.getInputStream();
InputStream stderr = process.getErrorStream();
// check the streams for errors
} else {
// log error
}
hth
Related
I found my command line tool gets stuck in between and needs a enter to process and I found that it’s because of "QUICKEDIT" mode in cmd and we want to disable it to avoid that. So I searched for Java options to disable quick edit mode on my app launch but I got only bat file from here quickedit.bat.
And this bat file works perfect when I run from my command prompt it disable quick edit mode in the current session itself which is the same I want. So I kept that bat file in my folder via installer and run it first on every launch but it’s not turning off the quick edit mode for current session.
I have tried using both process builder and runtime.exec.
Below is my code
ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "quickedit.bat");
File dir = new File(System.getProperty("user.home")+File.separator+"AppData"+File.separator+"Local"+File.separator);
pb.directory(dir);
Process p=null;
try {
p = pb.start();
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
try {
while ((line = in.readLine()) != null) {
System.out.println(line); // ----Here i get the same output i get when i run the bat file
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader inerr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
try {
while ((line = inerr.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
It gives me this:
When I run my bat file directly like this:
But through Java it didn't disable quick edit in my current command prompt whereas it disables at once I run the actual bat file. So can anyone say the reason or how to fix it or any other way to disable it for ever from Java?
Try pb.inheritIO(); before you call its start method. What you seem to have is a hybrid batch/Powershell script that that relies on stderr to determine which of the two it executes so this should require correct processing of stderr.
I didn't look at your bat file the most common issue on this kind of think is that process run from java did'nt share the environnement variable of your local setup nor jvm a quick fix to verify that is :
pb.environment().putAll(System.getenv());
hoping this will work :) then you just have to found which specific environnment variable is missing :)
I have created a java class to execute a batch file that is in my desktop so that the commands in the batch file will be executed too. The problem is that, i keep getting the error:
The filename, directory name, or volume label syntax is incorrect.
The filename, directory name, or volume label syntax is incorrect.
I have checked the .bat name and the directory. It is correct. When i type cmd /c start C:/Users/attsuap1/Desktop, windows explorer opens the desktop tab. However when i type cmd /c start C:/Users/attsuap1/Desktop/DraftBatchFile.bat, it gives the error. My DraftBatchFile.bat is in my desktop.
Here are my java codes:
public class OpenDraftBatchFile{
public OpenDraftBatchFile() {
super();
}
/**Main Method
* #param args
*/
public static void main(String[] args) {
//Get Runtime object
Runtime runtime = Runtime.getRuntime();
try {
//Pass string in this format to open Batch file
runtime.exec("cmd /c start C:/Users/attsuap1/Desktop/DraftBatchFile.bat");
} catch (IOException e) {
System.out.println(e);
}
}
Why is it that the batch file cannot be executed even if the directory is correct? Someone please help me. Thank you so much.
These are the codes in DraftBatchFile.bat
#echo off
echo.>"Desktop:\testing\draft.txt"
#echo Writing text to draft.txt> Desktop:\testing\draft.txt
When i execute the DraftBatchFile.bat by running the java class, i want a draft.txt file to be created in a testing folder that i have created (in desktop).
there is no such thing as desktop:\
Instead try something like this.
#echo off
echo . %userprofile%\Desktop\testing\dblank.txt
#echo Writing text to draft.txt > %userprofile%\Desktop\testing\dblank.txt
You just need to change a directory and create new subdirectory if it not exist. My offer is change .bat file like this:
#echo off
if not exist "%userprofile%\Desktop\testing" mkdir "%userprofile%\Desktop\testing"
echo.>"%userprofile%\Desktop\draft.txt"
#echo Writing text to draft.txt>"%userprofile%\Desktop\draft.txt"
Also you can create .txt file and write in it text through Java code:
File directory = new File(System.getProperty("user.home")+"//Desktop//testing");
if (!directory.exists())
directory.mkdirs();
String content = "Writing text to draft.txt";
File file = new File(System.getProperty("user.home")+"//Desktop//testing//draft.txt");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(content);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (writer != null)
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
How to install silently application in windows by java code. I have downloaded the file from server but need to install also on a single click.
How can I achieve this.?
If I understood it correct you want to install a third application from your java application. All you can do is below (this is for exe.. not sure about dll, I do not think you can run them). That should run the installable exe. But it will install or not that depends upon how that software works.. an give it a try.. But this is not recommended
public static void main(String args[]) {
try {
Process proc = Runtime.getRuntime().exec("your installable exe");
proc.waitFor(); //Wait for it to finish
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Try the following code:
String command = "C:\\setup.exe";
Runtime.getRuntime().exec("cmd /c "+command);
read more.
To run batch file try:
Add this to your batch file:
#echo off
C:\Windows\notepad.exe yourpath\omt.txt
In your java program:
String filePath = "C:/yourbatpath.bat";
try {
Process p = Runtime.getRuntime().exec(filePath);
} catch (Exception e) {
e.printStackTrace();
}
read this to get in depth idea.
I have created the following code below in Eclipse / java which executes a batch file, which in turn should also execute once all my TestNG tests have executed but sometimes the bat file will execute and sometimes it dosnt do anything at all, any ideas?
#AfterSuite(alwaysRun = true)
public void executeBatFile() {
try {
List cmdAndArgs = Arrays.asList("cmd", "/c", "copyPasteImgs.bat");
File dir = new File(Paths.get(System.getProperty("user.dir") + "/..").toRealPath() + "\\");
ProcessBuilder pb = new ProcessBuilder(cmdAndArgs);
pb.directory(dir);
Process p = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
}
The batch files moves files from a local folder to a remote folder (When the batch file hasnt worked via eclipse or invoked via jenkins I have manually executed the batch file and it did its jobs, very weird...)
thanks for your help
Apache Commons CLI provides functionalities to handle using command line from java. You can also use the exit value to have an idea about what is going on with your command. For example:
String command = "dir";
CommandLine oCmdLine = CommandLine.parse(command);
DefaultExecutor oDefaultExecutor = new DefaultExecutor();
oDefaultExecutor.setExitValue(0);
try {
int iExitValue = oDefaultExecutor.execute(oCmdLine);
} catch (ExecuteException e) {
System.err.println("Execution failed.");
e.printStackTrace();
} catch (IOException e) {
System.err.println("permission denied.");
e.printStackTrace();
}
I am trying to compile a jar file from within a Java program. When I run the code it starts to build the jar file and saves about 24k of it then the execution just seems to stop and wait (I am using Process.waitFor() and the program isn't finishing its execution). When I force the program to stop the size of the jar file jumps to about 45k. The jar should be about 1300k.
I tried creating a batch file and then calling the batch file with my ProcessBuilder but the same issue occurs. The batch file when run by itself works perfectly.
This is the code I have so far:
public static void buildJar() {
List<String> jarCmdArgs = new ArrayList<>();
jarCmdArgs.add("jar");
jarCmdArgs.add("cvfM");
jarCmdArgs.add(ROOT_DIRECTORY + File.separator + "my_jar.jar");
jarCmdArgs.add(".");
// jarCmdArgs.add("cmd.exe");
// jarCmdArgs.add("/C");
// jarCmdArgs.add(ROOT_DIRECTORY + File.separator + "make_jar.bat");
ProcessBuilder pb = new ProcessBuilder(jarCmdArgs);
pb.directory(new File(SRC_DIR_PATH));
try {
Process p = pb.start();
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
This is the contents of the batch file:
//cd to my working dir with all the files to put into the jar
jar cvfM my_jar.jar .