Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6919599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:00:41+00:00 2026-05-27T10:00:41+00:00

This is an extremely strange situation, but I just cannot point out what I’m

  • 0

This is an extremely strange situation, but I just cannot point out what I’m doing wrong.

I’m executing a big bunch of SQL scripts (table creation scripts, mostly). They are executed through Java, using sqlcmd. Here’s the sqlcmd command I use.

sqlcmd -m 11 -S SERVER -d DB -U USER -P PASS -r0 -i "SCRIPT.sql" 2> "ERRORS.log" 1> NULL

Note: I use the -r0 and redirects to make sure only errors go into the log file. I chuck out all STDOUTs.

Now I execute this command in Java, using getRuntime.exec(), like this.

Runtime.getRuntime().gc();
strCmd = "cmd /c sqlcmd -m 11 -S SERVER -d DB -U USER -P PASS -r0 -i \"SCRIPT.sql\" 2> \"ERRORS.log\" 1> NULL"
Process proc = Runtime.getRuntime().exec(strCmd);
proc.waitFor();

Note: I use cmd /c, so that the command runs in its own shell and exits gracefully. Also, this helps in immediately reading the error log to look for errors.

The Problem!

This command works perfectly when run by hand on the command prompt (i.e. the tables are getting created as intended). However, when executed through Java as shown, the scripts are run, and and there are no errors, no exceptions, nothing in the logs. But, when checking in SSMS, the tables aren’t there!

Where do I even begin debugging this issue?

UPDATE: I’M A MORON

The return value from the getRuntime().exec method is 1. It should be 0, which denotes normal execution.

Any pointers on how to fix this?

UPDATE 2

I’ve looked at the process’ ErrorStream, and this is what it has.

Sqlcmd: Error: Error occurred while opening or operating on file 2>
(Reason: The filename, directory name, or volume label syntax is
incorrect).

Looks like the path I’m passing is wrong. The error log goes into my profile directory, which is C:\Documents and Settings\my_username. Do the spaces in the path matter? I’m anyways double-quoting them!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:00:42+00:00Added an answer on May 27, 2026 at 10:00 am

    Have a look at the exec method with an string array as parameter:

     java.lang.Runtime.exec(String[] cmdArray)
    

    The JavaDoc for this method says:

    Executes the specified command and arguments in a separate process.

    So, the first item in the array is the command and all of your arguments are appended to the array, e. g.,

    Runtime.getRuntime().exec(new String[] {"cmd", "/c", "sqlcmd ... "});
    

    After looking at your comment and the implementation of exec(String) it seems to be, that the exec method recognizes the pipe operator > as an argument to cmd, because exec(String) splits the command string to an array using whitespaces as seperators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is extremely basic and I apologize for asking such a rookie question. But
This is extremely silly, but I can't for the life of me figure it
I am extremely confused why I am getting this strange error all the sudden:
I have this extremely strange behavior coming : In the below code: If I
This might be extremely trivial, and if so I apologise, but I'm getting really
I'm having an extremely strange problem with ant. This snippet produces a set of
Ok I must be overlooking something extremely simple but I am lost. Given this
I'm extremely new to JS and have this code that I'm trying to tweak.
This is a bit of a long shot, but if anyone can figure it
This is starting to vex me. I recently decided to clear out my FTP,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.