Open Excel file using Windows Task Scheduler - java

I have a Talend job that will open an Excel file when certain conditions are met. The Excel file has lots of VBA in it to read from SQL Server and create a document. I can run the Talend job successfully when running from Open Studio. I am now trying to schedule the job in Windows Task Scheduler that will run the Talend job every 5min to open the Excel file.
I tried using a tJava component to use the Desktop class to open the file, but that did not work.
Desktop dt = Desktop.getDesktop();
dt.open(new File("C:/Users/<username>/<filepath info>/TEST.xlsm"));
Now, I'm trying to use a tSystem component with the following command:
"cmd.exe /c start excel \"C:/Users/<username>/<filepath info>/TEST.xlsm\""
I believe it does not work due to the fact that when scheduled, it becomes a background process that has no reference to a desktop or cmd that it can run the command on. How can I open my Excel file from a background job using Java?

If using the start command and the path of the file to be started contains a space then you must specified a title to the start command.
import java.io.IOException;
class StartExcel {
public static void main(String args[])
throws IOException
{
String fileName = "c:\\temp\\xls\\test2.xls";
String[] commands = {"cmd", "/c", "start", "\"DummyTitle\"",fileName};
Runtime.getRuntime().exec(commands);
}
}
It's feature.

Related

Running batch file in java

I am trying to run a batch file using java. The batch file in turn runs a python program. So i should wait till the batch file is done and then proceed with my program.
Problems facing:
I could not run batch file in background. I am able to run it only via start
Process p = Runtime.getRuntime().exec("cmd /c start c://GCTI//IA/QAART//testercheck.bat");
once the batch file ran, it is not closing automatically.
Batch file
"C:\Python27\python.exe" -i "C:\GCTI\IA\QAART\tester\test_monitor.py" -init "C:\GCTI\IA\EpiPhone\Dispatcher6\init\INIT_Designer_QAART_Dispatcher_Chat.PY" -testlist "C:\GCTI\IA\ASR_QAART\dat files\ChatAutomation\chat.dat" 23
Can you please help me to run this batch ile in background?
You don't need the batch file. You can execute the Python program directly from Java code using class java.lang.ProcessBuilder.
ProcessBuilder pb = new ProcessBuilder("C:\\Python27\\python.exe",
"-i",
"C:\\GCTI\\IA\QAART\\tester\\test_monitor.py",
"-init",
"C:\\GCTI\\IA\\EpiPhone\\Dispatcher6\\init\\INIT_Designer_QAART_Dispatcher_Chat.PY",
"-testlist",
"C:\\GCTI\\IA\\ASR_QAART\\dat files\\ChatAutomation\\chat.dat",
"23");
Process p = pb.start();
int result = p.waitFor();
Refer to other methods in class ProcessBuilder for handling the output of the Python script, for example method inheritIO

Open webpage from a Java application doesn't work when run from task scheduler

I wrote a Java program some time ago that automatically opens a webpage before I get to work in the morning. I do this by calling a batch file from Java, which opens the webpage. This program worked great for about three or four months without any problems, but one day it just stopped working. I have tried opening the webpage from within Java as well instead of the batch file, but I consistently run into a problem. This is the process I use:
I exported my program as a runnable JAR and converted it to an EXE using Launch4j. In my Windows task scheduler I schedule it to run at 5:30am every morning.
My Java program calls the batch file that is stored on my desktop
The batch file opens Chrome and calls the webpage to open
When I run the Java program EXE manually (double-click from the desktop) it runs correctly, calls the batch file, and opens the webpage. However, when I try running the EXE from the Windows task scheduler it does not (visibly) open the webpage. It does appear to run Chrome in the background (according to the task manager/Process Explorer) but the webpage itself doesn't seem to open (I can tell because the webpage I open should be playing music, but I hear nothing).
This is the Java function I use to run the batch file:
public static void openWebpage() throws Exception {
String[] startupBat = {"cmd", "/c", "start", "/B", System.getProperty("user.home") + "\\Desktop\\WebpageStart.bat"};
ProcessBuilder pb = new ProcessBuilder(startupBat);
try {
webpage = pb.start();
} catch (IOException e) {
logWriter.write(tf.format(new Date()).toString() + " - Unable to start webpage");
}
if (webpage != null) {
try {
webpage.waitFor();
} catch (InterruptedException e) {
logWriter.write(tf.format(new Date()).toString() + " - Webpage startup interrupted");
} finally {
webpage.destroy();
}
}
}
And here are the contents of the batch file I use to start the webpage:
#echo off
start "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" http://www.myWebpage.com
Once again, everything works fine when I run the Java EXE manually, but when I try to run it from the task scheduler the webpage doesn't appear to open. What could cause this sort of behavior? What changes when the task scheduler runs a program vs. when it is manually run?
A few things to check
Which user is the scheduled task running as? Is it the same user as the one double clicking on the icon?
Have you changed your password recently? You might need to update the password on the scheduled task
Go to the windows event viewer, are there any errors in there?

Execution of Windows batch file using Java ProcessBuilder always returns exit code 0

I'm trying to execute a batch file and get the error code from it in Windows 7 Enterprise 64-bit.
My batch file is c:\test.cmd and contains a single line:-
exit 1
My code for executing the batch file is:-
public static void main(String[] args) throws Exception {
Process process = new ProcessBuilder("c:\\test.cmd").start();
System.out.println(process.waitFor());
}
The output is zero. If I try with:-
new String[] {"cmd", "/c", "c:\\test.cmd"}
the result is again zero.
There doesn't seem to be much magic to the ProcessBuilder API that I'm missing. Can anyone see where my code is going wrong?
Shouldn't I be able to capture the exit code of the batch file?
I think there is something wrong (or different) with my PC. The Apache Commons Exec project source code I downloaded failed unit tests when capturing return codes. Looks to be unsolvable on my PC and haven't found a workaround.

exec not working with java 1.7.21, but in netbeans works fine

I made a little program and it worked fine, but now. First, it mux the xml chapter file in the mkv file, so we get a muxed mkv file. Some day ago I updated java to 1.7.21 and I think this is the problem why it is not working now. It's a little strange, but when I run in netbeans everything is fine, but when I build and I run the .jar file, it is not working. It create the xml file, but not mux in the mkv file (and because not muxed not delete the xml file). Here is the code: (filename=xml file path; mkv=mkv file path)
public void muxing() {
try {
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("c:\\Program Files\\MKVtoolnix\\mkvpropedit.exe --chapters \""+filename+"\" \""+mkv+"\"");
if (p.waitFor()==0) {
File xmlfile=new File(filename);
xmlfile.delete();
}
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
The program worked with java 1.6 and I think with 1.7.17 too. Win7 32bit. Sorry for my bad English.
Oracle has made breaking changes to Runtime.exec() in Java 7 update 21 (and 6 update 45).
If the program name contains spaces, you need to specify command and arguments in an array:
Process p = Runtime.getRuntime().exec(new String[] {
"C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\""});
Another option is to use java.lang.ProcessBuilder:
Process p = new ProcessBuilder("C:\\Program Files\\MKVtoolnix\\mkvpropedit.exe",
"--chapters", "\""+filename+"\"", "\""+mkv+"\"").start();
As stated by Oracle:
Applications that need to launch programs with spaces in the program name should consider using the variants of Runtime.exec that allow the command and arguments to be specified in an array.
Alternatively, the preferred way to create operating systems processes since JDK 5.0 is using java.lang.ProcessBuilder. The ProcessBuilder class has a much more complete API for setting the environment, working directory and redirecting streams for the process.

Start Windows Service with Java

I am trying to start a windows service in java using this
public static void main(String[] args) throws IOException {
String startCom = "net start";
String startProc = "\"C:/Program Files/Common Files/Apple/Mobile Device Support/bin/AppleMobileDeviceService.exe\"";
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(startCom + startProc);
System.out.println("Starting It");
}
It runs with no exceptions but does not start the service. What Am I doing wrong?
Try to figure out what the registered service name is, and use that instead of the full executable. For example:
net start "Adobe Acrobat Update Service"
You can find out the service name by by running net start on a command window (which prints a list of all registered services) or by finding the service in the Services control panel by clicking the Start button, typing services.msc, and pressing Enter. If the service name is cryptic, you can right-click the service in the Services control panel and click Properties to confirm the executable for that service.
You probably need to execute the command with escalated privileges. You can either do this by disabling UAC (not recommended), by launching javaw.exe with elevated privileges when you start your program, or by using a utility like Elevate.exe to execute any privileged commands.
If you're having trouble getting Runtime.exec to do your bidding, try using ProcessBuilder instead.
Lastly, it's a good idea to always read the contents of STDOUT and STDERR (from Process.getOutputStream() and Process.getErrorStream()). They might contain diagnostic information; but even more importantly, if the buffers fill up while the Process is still outputting to them, the Process will hang.
Try this code:
public static void main(String[] args) throws IOException {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("cmd start /c C:/Program Files/Common Files/Apple/Mobile Device Support/bin/AppleMobileDeviceService.exe");
System.out.println("Starting It");
It looks like your main() exits, so your service will die. In this case, you need to install the service using
sc create "servicename" binpath="path",
and then start it with
sc start "servicename.
That is, a service .exe still needs to be installed as a Windows service.

Categories

Resources