Open Excel from Java Application - java

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.

Related

java.io.FileNotFoundException on Properties FileInputStream in CMD

I'm loading a Property file with the following method:
private final String utitt=Paths.get(".").toAbsolutePath().normalize().toString();//
...
properties.load(new FileInputStream(utitt+"/beallitasok/lang.set"));
...
This works fine within NetBeans without problems, but the jar file fails in command prompt:
java.io.FileNotFoundException: C:\java\IKE\dist\beallitasok\lang.set
(A rendszer nem találja a megadott fájlt)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at IKE.IKE.fordit(IKE.java:1519)
at IKE.IKE.access$300(IKE.java:81)
at IKE.IKE$IngatlanokAblak.(IKE.java:9411)
at IKE.IKE.(IKE.java:544)
at IKE.IKE.main(IKE.java:697)
I've copied the "beallitasok" directory there (to c:\java\IKE\dist in my case), so the file is actually there. I'm using some files to translate certain strings and save/read some settings. I've checked whether i'm just blind, so just opened the file with a "more C:\java\IKE\dist\beallitasok\lang.set" command and it works.
Where or how shall i search for a clue? I'm not a pro, so thanks for any help.
PS: (A rendszer nem találja a megadott fájlt) = The System can't find the specified file. Translated from the Hungarian language.

Executing Windows shell commands using 'not-installed' Java

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

How do I use mysqlimport?

Here is the command I am running using Runtime.getRuntime().exec() in Java:
mysqlimport --fields-terminated-by=, --lines-terminated-by="|" --local
--user=u --password=p DatabaseName
txtpath
Here is the error I get:
java.io.IOException: Cannot run program "mysqlimport": 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 databaseCommunication.UploadThread.run(UploadThread.java:66)
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
It seems like the problem should be that mysqlimport.exe is not installed or is not installed in the correct place, but I have tried downloading the mySQL utilities from http://dev.mysql.com/downloads/windows/installer/5.6.html and from https://dev.mysql.com/downloads/utilities/.
In order to make sure the problem was not that it could not find the file at "txtpath," I typed the full path into the command prompt, and the correct file was opened, so the error is definitely referring to mysqlimport.exe.
Googling my problem, the only threads I've been able to find refer to something called "Sqoop" which I am not familiar with, and they usually recommend downloading the mysql utilities.
For more context, I have been using BCP to upload data from a txt file to a sql server database, but now I need to do the same thing with mysql. If there is any way to use BCP (I'm pretty sure there isn't) or something else to bulk upload data from a local file I would be open to hearing that as well.
EDIT:
I am using Windows 8 on a remote desktop. I have manually added mysqlimport.exe to the PATH environment variable and it still gives the same error.
If you are on Windows, then try putting mysqlimport.exe as the command rather than just mysqlimport. If that still does not solve your problem, make sure that 'mysqlimport.exe' is in your PATH environment variable.

ffmpeg running on windows with java

I have simple test class as
public static void main(String[] args) throws IOException {
String[] line = {"ffmpeg -i D:\\hadoop-video\\testVideo\\xyz.mp4 %d.png"};
Runtime.getRuntime().exec(line);
}
when I try to run this I am getting
Exception in thread "main" java.io.IOException: Cannot run program "ffmpeg -i D:/hadoop-video/testVideo/xyz.mp4 %d.png": 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 ImageTest.main(ImageTest.java:13)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
however file is present on my windows7 machine with location
D:\hadoop-video\testVideo\xyz.mp4
i tried removing .mp4 and then run also not working . please suggest what might be wrong
Where ffmpeg.exe is installed? try to use full path to execute the ffmpeg.exe
e.g.
D:\ffmpeg\bin\ffmpeg.exe
then
String cmd[] = {"D:\\ffmpeg\\bin\\ffmpeg","-i","D:\\ffmpeg\\hadoop.mp4","D:\\ffmpeg\\img%d.png"};
Runtime.getRuntime().exec(cmd);
or
Process process = new ProcessBuilder(Arrays.asList(cmd)).start();

How to run setup.exe file using java

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);

Categories