I was writing a program to open notepad application from java code.
I was able to do it with below code:-
String command = "notepad.exe";
Runtime run = Runtime.getRuntime();
run.exec(command);
It opens the application but it doesn't open in maxzimized/full-screen mode.
Is there anyway in which notepad application can be opened in full-screen mode using java or in a specified size.
Yes you can with the help of Start and /Max argument,
Code
Process p = Runtime.getRuntime().exec("cmd /c START /MAX notepad.exe");
Caution
The code wont work if you dont use cmd /c before the command and it will lead you to the java.io.IOException because it cannot find the file specified.
Try something like this to give a dynamic Mode capability :-
String cmd = "notepad.exe /C START /MAX";
Runtime.getRuntime().exec(cmd + args[0]);
Runtime example :-
java -cp . StartWindowMaximized notepad.exe
Related
I am trying to execute cmd commands inside a java program using the following code
String command = "clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Process p1 = Runtime.getRuntime().exec(command);
This is executing in java without any exceptions, but the actual command is not running. If the command is run it should create text file foodout.txt in the location mentioned. Nothing is happening.
The actual command is
clingo food1.lp fooddata.txt 0 >>foodout.txt
clingo is a windows executable program. This command works fine when run in command prompt. I want to run this inside java program from click of a button. I have set environment variable for clingo. Clingo and this java project are in the same directory.
Before this i tried below code
String[] command = {"clingo", "food1.lp","fooddata.txt", "0", ">>foodout.txt"};
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File(WorkingDirectoryArea.getText()));
Process process = builder.start();
where Workingdirectoryarea contains the directory location for commands to be run. This code does nothing.
Can someone guide me or provide code sample on how to run the cmd command inside this java program. I am using Netbeans IDE. Thanks.
you said your command works with a command prompt. OK. If you look closely, the command window has a path entry (cmd= echo %PATH%). That's the difference between executing a command in a command window and executing a java process. You have 2 options.
1. Add the path to the process.
2. Add the path to the clingo command (i.e. "f:\path\clingo.exe ...)
Item 1 is especially needed when using dos commands. To add a path environment:
Runtime.getRuntime().exec not finding file in java environment
You are redirecting standard output to a file. This is not part of the command nor a command line parameter. Is the command interpreter that handles this.
You must invoke the command interpreter to run your program like this:
String command = "cmd /c clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Process p1 = Runtime.getRuntime().exec(command);
Note the cmd /cpart which invokes the command interpreter to run your command like you would do on a Windows terminal.
On Linux it would be sh -c or whatever shell you like.
EDIT 1
When running the command, clingo.exe must be in your path or it must be in the default directory for the Java interpreter. If not, you should give the full path to the executable, like this:
String command = "cmd /c F:\\clingo\\clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
+ " 0"+" >>F:\\clingo\\foodout.txt";
Try to run
F:\\clingo\\clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt 0 >> F:\\clingo\\foodout.txt
at a Windows prompt and see if it works as expected. If it works it also should work when run from a Java program. Please, replace the clingo path with the right one for your environment.
Your command must be like this: java -jar yourExecuteable.jar yourParameter
In your case: java -jar clingo.jar food1.lp fooddata.txt 0 >>foodout.txt
I'm trying to open a PowerPoint file from a Java program using this method
Desktop.getDesktop().open(new File(pPTFile));
it's opening on maximum size but I want to open it minimize
Any ideas ?
There is a way in windows to open any application in the minimized state, use this command and pass the powerpoint exe and ur ppt file as and argument to the start command:
Command line version: cmd /c start /min POWERPNT.exe e:\test.pptx
Java code:
Runtime.getRuntime().exec(new String[]{"cmd","/c", "start", "/min", "POWERPNT.exe", "e:\test.pptx"});
I am in the midst of a Java project, part of which is calling the Windows cmd to make a directory. My code currently looks like this:
Runtime rt = Runtime.getRuntime();
String command;
command = "cmd.exe /c start mkdir \"C:\\Users\\User1\\Documents\\Folder1\\"+folderName+"\" &&exit";
rt.exec(command);
This works fine (creates the folder), but it spawns an additional instance of cmd. (I originally added the "&&exit" thinking it would eliminate the extra window, but I now realize it is unnecessary code.)
1) Can I prevent this additional instance of cmd (which begins in the new directory), or
2) Can I close this extra cmd window without causing other problems? (I have heard that killing cmd can break other things on a machine.)
You should use:
File file = new File("C:\\Users\\User1\\Documents\\Folder1\\"+folderName+"\"");
if(!file.exists())
{
file.mkdir();
}
instead. However, if you want to call the command into cmd without creating a new one, you should not call "cmd.exe /c start". You can check that if you run that very same command from outside java it will also start a new cmd. Try this:
Runtime rt = Runtime.getRuntime();
String command;
command = "mkdir \"C:\\Users\\User1\\Documents\\Folder1\\"+folderName+"\" &&exit";
rt.exec(command);
Why don't you create the dir with File?
new File("C:\\my\\path\\myDir").mkdir();
I am newbie for calling scripts from Java through the SFTP connection.
So far, I managed to find the code snippets below
Process p = Runtime.getRuntime().exec("cmd /c start Hello.bat");
p.waitFor();
Here is sample for Hello.bat
#echo off
echo "Hello World"
However, I couldn't see the output in cmd window eve there were no errors.(seem like Hello.bat file location is not right?)
My actual and final script include copying, reading, archiving, delete and return code for success or fail.
What type of script with the above methods will be fine and I hope some one will advise me for the right direction with working sample.
Thanks and best regards
just Runtime.getRuntime().exec("cmd /c start Hello.bat") works for me. Also see How do I run a batch file from my Java Application?
Make a small change in batch file to keep command prompt open
#echo off
echo "Hello World"
PAUSE
so your java code is fine you just need to modify batch file to keep command prompt open when program is run successfully.
I would like to know how to enter commands into the cmd.exe (command prompt window) , after its been opened?
I have the code below to open cmd.exe:
Runtime rt= Runtime.getRuntime();
Process process= rt.exec("cmd.exe /c start cd c:\\ExecutionSDKTest_10.2.2");
But after it's been opened, I'd like to enter "ant compile" or any line, how do I do that??
The normal way to do this would be to put the commands in a script and execute the script.
You will need to consume the output of the child process (stdout and stderr) on separate threads, or your process will block.
you dont need to open the command line to compile a program with a running program, check this out how to compile & run java program in another java program?
Not exactly the answer to you question. But you can use ProcessBuilder to set your process current directory (so you don't need to call "cd ..." anymore)
Try to pass a List to the ProcessBuilder
final List<String> l = new ArrayList<String>();
final String cmd = "C:/Program Files/Java/jre6/bin/";
l.add("C:\\WINNT\\system32\\cmd.exe ");
l.add("cd " + cmd);
l.add("dir");
l.add("java.exe -version");