I can't install Java on the computer, I only have a 'Java folder' on my desktop. Because of that executing 'cmd.exe' commands with Java doesn't work. Is there any method of pointing to the 'cmd.exe' and telling java to run a command using it?
I mean somethind similar to what I do in my 'start.bat' file i use to run .jar's - start C:\Users\kamilkime\Desktop\JEclipse\Java\jdk1.7.0_75\bin\java -jar Calculator.jar
I tried using this two methods, both don't work and throw the same exception:
//Method 1
Runtime.getRuntime().exec("cls");
//Method 2
ProcessBuilder builder = new ProcessBuilder("cls");
builder.start();
//Exception stacktrace
java.io.IOException: Cannot run program "cls": CreateProcess error=2, Nie można odnaleźć określonego pl
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1047)
at java.lang.Runtime.exec(Runtime.java:617)
at java.lang.Runtime.exec(Runtime.java:450)
at java.lang.Runtime.exec(Runtime.java:347)
at com.gmail.kamilkime.Main.clearConsole(Main.java:82)
at com.gmail.kamilkime.Main.start(Main.java:61)
at com.gmail.kamilkime.Main.main(Main.java:36)
Caused by: java.io.IOException: CreateProcess error=2, Nie można odnaleźć określonego pl
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:385)
at java.lang.ProcessImpl.start(ProcessImpl.java:136)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1028)
... 6 more
//edit 08.10.2015
Ok, so thanks to #9dan I've managed to run a console window, but... Now I can't do anything with it - cannot println() a message, cannot get a user input. Is it posibble to do sth like that (printing/getting input)?
BTW, the thing I'm trying to do the whole time is finding a way to enable the console program by double-clicking the .jar file, not by using 'java' command in 'cmd.exe'.
Now, I'm using this code and I'm stuck - I don't know what to do: http://pastebin.com/VLkr1RFd
Related
If I am tring to open notepad from Java Application then it will open Notepad.But If I will try to open Excel then it is giving me exception.
try
{
Runtime.getRuntime().exec("excel");
}
catch (IOException e)
{
e.printStackTrace();
}
Following is Exception :
java.io.IOException: Cannot run program "excel": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at OpenNotepad.main(OpenNotepad.java:18)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more
When I do start->Run->Excel then it will open excel.
If you are trying to use Java to open an Excel file, rather than just opening Excel, I suggest you use the Desktop API class: http://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html
This class will use the default OS file handling mechanism, so it will use say MS Excel in Windows, and Open Office in Linux.
Runtime.getRuntime().exec("some command"); does the same as "some command" will do in cmd. So the solution for opening Excel you should try this.
try {
Runtime.getRuntime().exec("cmd /c start excel.exe");
} catch (IOException e) {
e.printStackTrace();
}
in the exec method give fuuly qualified path where your excel is installed and then try and RunTime is good but better to use ProcessBuilder
Runtime.getRuntime().exec() is the same thing as doing something from the command line.
In windows OS there is an environment variable set to C:\Windows\System32
all the exe of commands are in this path.
when I do following in cmd I get this.
C:\Users\bhaviksh>echo %PATH%
D:\oracle\product\10.2.0\client_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\
System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MySQL
\MySQL Server 5.0\bin;C:\Program Files\cvsnt;C:\Program Files\TortoiseSVN\bin;C:
\maven\bin
Solution : use fully qualified name of exe file
Add microsoft office excel executable path
for eg:
If Directory C:\Program Files (x86)\Microsoft Office\Office14 has EXCEL.EXE then add C:\Program Files (x86)\Microsoft Office\Office14 to your classpath.
How to add to classpath Click here.
Once you are done, restart your IDE, your program should work.
Calling R in java-Rcaller
After following above link I've created new "myScript.R" seperately.
Please tell me that it is showing "save workspace" for saving. I've saved it as file with .R extension. Please correct me if I'm wrong.
I'm recieving the following error on running this as same Java program (provided in above link) created in Netbeans IDE for connecting RCaller with Java:
run:
java.io.IOException: Cannot run program "Rscript": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at javaapplicationrcaller2.JavaApplicationRCaller2.<init>(JavaApplicationRCaller2.java:41)
at javaapplicationrcaller2.JavaApplicationRCaller2.main(JavaApplicationRCaller2.java:25)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:288)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 5 more
BUILD SUCCESSFUL (total time: 0 seconds)
I am developing an java application from which I have to run xyz_setup.exe installer. I tried the following code
String command = "C:\\xyz_setup.exe";
Runtime.getRuntime().exec(command);`
But it was throwing the following error
java.io.IOException: Cannot run program "C:\Users\NewtonApples\Downloads\idman614.exe": CreateProcess error=740, The requested operation requires elevation
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at upendra.OpenExternalApplication.main(OpenExternalApplication.java:19)
Caused by: java.io.IOException: CreateProcess error=740, The requested operation requires elevation
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:189)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1021)
... 4 more
Can any one suggest me how to do this?
Java (or likely any other process which uses CreateProcess system call directly) is not good with executables requiring access elevation.
You can get around that by executing your program via shell:
String command = "C:\\setup.exe";
Runtime.getRuntime().exec("cmd /c "+command);
I am trying to run there own example of LegacyLibrary on http://code.google.com/p/javacpp/
when I try to compile code with following command mentioned at site only (basically compiling code with javacpp.jar) I am getting following exception
D:\Java Workspace\POC\JavaCPP\bin>java -jar javacpp.jar LegacyLibrary
Generating source file: D:\Java Workspace\POC\JavaCPP\bin\jniLegacyLibrary.cpp
Building library file: D:\Java Workspace\POC\JavaCPP\bin\windows-x86\jniLegacyLibrary.dll
cl "/IC:\Program Files (x86)\Java\jdk1.6.0_03\include" "/IC:\Program Files (x86)\Java\jdk1.6.0_03\include\win32" "D:\Java Workspace\POC\JavaCPP\bin\jniLegacyLibrary.cpp" /W3 /Oi
/O2 /EHsc /Gy /GL /MD /LD /link "/OUT:D:\Java Workspace\POC\JavaCPP\bin\windows-x86\jniLegacyLibrary.dll"
Exception in thread "main" java.io.IOException: Cannot run program "cl": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at com.googlecode.javacpp.Builder.build(Builder.java:189)
at com.googlecode.javacpp.Builder.generateAndBuild(Builder.java:234)
at com.googlecode.javacpp.Builder.main(Builder.java:479)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 3 more
what the remedy for this ?
The error message is pretty clear: it's not finding the cl executable, which is the name of the Visual Studio compiler.
You should run that command from a Visual Studio command prompt (there's an entry in the start menu for that, usually), to get the right environment variables loaded.
I am using jdj 1.7,netbeans 7 versions. I wrote a program that's working fine. Now i want to clear the console window after many outputs in the console. I tried the below line Runtime.getRuntime().exec("cls"); also tried with exec("clear"). But i am getting these errors:
Exception in thread "main" java.io.IOException: Cannot run program "cls": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at Periodical.main(Book.java:88)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
What's the problem here. How can i clear the console? Thank you
If you want to keep it portable you might want to use a library like jcurses. That will save you the trouble of having to detect what kind of console/terminal you are on and will give you a lot of other nice features like cursor and color control.