The trouble is, the Buddha never said it. The passage is an extract from "The Gospel of the Buddha" written by Paul Carus in 1894. The "Gospel" was one of the early popularizing works which introduced Buddhist thought to the West. Like the "Light of Asia" by Edwin Arnold written around the same time, these works served a useful purpose and were many people's first encounters with Buddhism. Unfortunately, both Carus and Arnold too often let their own ideas intrude and put their own words in the Buddha's mouth.
The Tathagata having given his consent, Simha continued: "I am a soldier, O Blessed One, and am appointed by the king to enforce his laws and to wage his wars. Does the Tathagata who teaches kindness without end and compassion with all sufferers, permit the punishment of the criminal? and further, does the Tathagata declare that it is wrong to go to war for the protection of our homes, our wives, our children, and our property? Does the Tathagata teach the doctrine of a complete self-surrender, so that I should suffer the evil-doer to do what he pleases and yield submissively to him who threatens to take by violence what is my own? Does the Tathagata maintain that all strife, including such warfare as is waged for a righteous cause should be forbidden?"
The Buddha replied: "He who deserves punishment must be punished, and he who is worthy of favor must be favored. Yet at the same time he teaches to do no injury to any living being but to be full of love and kindness. These injunctions are not contradictory, for whosoever must be punished for the crimes which he has committed, suffers his injury not through the ill-will of the judge but on account of his evildoing. His own acts have brought upon him the injury that the executer of the law inflicts. When a magistrate punishes, let him not harbor hatred in his breast, yet a murderer, when put to death, should consider that this is the fruit of his own act. As soon as he will understand that the punishment will purify his soul, he will no longer lament his fate but rejoice at it."
The Blessed One continued: "The Tathagata teaches that all warfare in which man tries to slay his brother is lamentable, but he does not teach that those who go to war in a righteous cause after having exhausted all means to preserve the peace are blameworthy. He must be blamed who is the cause of war. The Tathagata teaches a complete surrender of self, but he does not teach a surrender of anything to those powers that are evil, be they men or gods or the elements of nature. Struggle must be, for all life is a struggle of some kind. But he that struggles should look to it lest he struggle in the interest of self against truth and righteousness. LINK
A little Googling and I discovered that this passage from Carus is posted all over the net, usually in compilations of basic Buddhist doctrines. This is troubling for many reasons. The last thing the world needs now is a Buddhist justification for war. It also points out the scholarly sloppiness of so much material on the internet. The Carus passage is quoted here and there without indicating the source. The language is such that it would fool many into thinking it a quote from scripture.
In general I think we could learn a trick from the Christians here. Too many Buddhist books and websites quote the "Buddha" without identifying the sutta. Even if it's a genuine quote, it's frustrating if one wants to check the source or the translation if it isn't cited. Christian works are full of chapter-and-verse numbers after every biblical quote.
To many people, the Buddha becomes a screen on which they project their own ideas. ("He was enlightened, right, so he has to have agreed with me") The Buddha was a real person, a specific teacher with specific teachings and not a foil for every cranky idea that comes down the pipe.
And for the record, there is no such thing as a "Just War" doctrine in Buddhism. If you want one, you're looking in the wrong religion.
LINK - A well argued and well cited article about "Just War" and Buddhism.