Hi All,

I am attempting to execute a dtsx package using Java and I have the following line

Process executionProcess = Runtime.getRuntime().exec("dtexec/f\"C:\\Projects\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\ConferenceBoardETL.dtsx\"");

I keep getting an error saying the file cannot be found but if I run the dtexec in command prompt with the exact same paths and file name it runs. Can someone tell me why it won't work in Java? Please don't grill me over the folder naming convention as it is not by my design. :)

The exec method does not necessarily use your command the same way that your command prompt uses it. Practically everything that exec does is operating system dependent, so it's hard to make any promises about how it should be used, but you shouldn't assume that it is as sophisticated as your command prompt.

There is at least a vague concept of a command and the command's arguments that you seem to be ignoring, which might be the cause of your problem. You are giving exec the command and its arguments run together all in one word instead of putting spaces between dtexec and /f and the file name. Apparently your command prompt has no problem with that, but to the exec method that means the whole thing gets treated as a command with no arguments even though it looks like dtexec should be the command and /f and the file should be the arguments.

Even if you do separate the arguments there are no guarantees because this entire problem is operating system dependent. You might also want to try removing the quotes from around the filename because your command prompt might be removing the quotes before passing the file name to dtexec, and the exec method probably won't do that. On the other hand if the command prompt isn't removing the quotes and dtexec requires the quotes, then you need to have the quotes.

With luck, a little trial-and-error should quickly solve the problem.

Thanks bguild, I have to use the quotes because Netbeans doesn't like that line without them. When I get to work in the morning I will play around with the spacing. I appreciate your input on this and hopefully the spacing is the problem.

The version of exec that takes a single String as its argument is full of problems if you have a command with arguments, or spaces or quotes anywhere in it. It tries to parse the command/arguments itself and often gets it wrong.
There is a version of exec that takes a String array as its parameter. Put the command (executable) in the first element, and the argument(s) in the following element(s). No need to put quotes round any of them. This avoids all those problems.

It tries to parse the command/arguments itself and often gets it wrong.

It simply uses whitespace to separate the command from the arguments and the arguments from each other. There's nothing wrong with that since it's such a clear and simple rule so that anyone who has read the documentation will understand it. The real problem with exec is that all the rest of it is not properly documented, so we understand how it parses commands and arguments better than anything else that it does.

In the absense of quote marks that's true, but if, for example you have a path for the executable that contains blanks (eg default Windows 7) you have major problems. I'm not saying that it's parsing is unclear or undocumented. I'm saying that it often does not do what you want or need.

Is there another way to execute a dtsx package with Java? I just got into work and am going to play around with the spacing but if there's another way to execute this, better, I would love to see it.

ProcessBuilder is nowdays recommended over Runtime.exec, but in this case there's probably no real advantage. The main thing is to pass the dtexec and its parameters in separate elements of a String array, not just as one String.

Being a Java noob (ish) kind of guy, can you show me what you mean by passing through a String array? I see different exec calls that I can make but I'm unsure how to pass this particular string properly.

Something like this...

String[] runParameters = {"command", "arg 1", "arg 2"};
// array of strings, first is command, following are the args required for that command

Thanks JamesCherrill. I tried:

String[] runParameters={"dtexex/f ","C:\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard","ConferenceBoardETL.dtsx"};

However I still get the following message:

Cannot run program "dtexex/f ": CreateProcess error=2, The system cannot find the file specifiedQuoted

The file is definitely there, I'm stumped!

I'm not familiar with dtexec, but isn't /f part of the arguments, not part of the command itself? And isn't the final .dtsx part of the first argument?
I would have guessed
{"dtexex","/f C:\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\ConferenceBoardETL.dtsx"};
(note space between /f and c:), or maybe
{"dtexex","/f", "C:\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\ConferenceBoardETL.dtsx"};

commented: Thank you for trying to see me through this, I really appreciate it! +2

Sigh, either way I'm still not getting the results I need. I wish IT would let me program using .NET in Visual Studio. I had it for a spell but they took it away from me because they don't want "students" using Visual Studio. I suspect this is an easier call from a VB perspective although I'm not certain.

LoL, "dtexec" not "dtexex", the following works :)

String[] runParameters={"dtexec","/f C:\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\EconAnalysisConferenceBoard\\ConferenceBoardETL.dtsx"};

Thanks a lot for helping me nudge my way through this. I'm happy to say this thread is solved!!!

I thought this was solved but, the class builds successfully but doesn't actually execute the dtsx file.

Well it turns out my path was slightly wrong. It's working 100%.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.