When is violence justified?
Defining violence as the deliberate harm of another human being, when violence is justified, if ever, depends upon religion, culture, law, and one's personal philosophy or beliefs. The answer to this question will be dependent upon some or all of those variables and will range from "Never" to "Any time I feel like it is."
There are religions whose tenets are at least in theory pacifistic. For example, in Christianity, one is counseled "to turn the other cheek" when someone has offended in some way. In the Quaker religion, violence is never justified, to the degree that Quakers will not serve in any military capacity. But for the most part, most major world religions justify violence, when under attack, certainly, and historically, to persuade others to be "saved" in the religion, as in the Crusades or the Spanish Inquisition. In the modern world, Israel, as a Jewish nation, justifies violence against its neighbors, some of whom have attacked it repeatedly, and various groups of radical Islam perceive their religion to be under attack and have terrorized the world with their violence. This is not representative of most people of this faith, though, in spite of all the publicity engendered by the few.
On a cultural level, there are societies in which a violent act will engender a violent response as a matter of honor. This seems to be true in various groups in Afghanistan, in prisons all over the world, and even in the mountains of Appalachia, where two warring families, the Hatfields and the McCoys, nearly destroyed one another with violence meant to uphold their honor. In each of these instances and many others, the violent actors feel completely justified because of the culture in which they live.
Laws in western culture for the most part justify personal violence to save oneself or to save others when the risk of serious harm or death is imminent, while at the national level, i.e., the violence of war, justifications are more complex. Self-defense is justifiable homicide, sometimes a defense sufficient to avoid charges completely and other times used at trial for a finding of not guilty. Countries that are under attack will always find justification to use violence in their own defense, but there is no question that countries will wage war without this justification, to gain territory, to gain power, or to gain natural resources. History is full of such instances, as are current times.
At a personal level, one's beliefs and philosophy determine what violence is or is not acceptable and justified. I think parenting provides a good example of this. There are those who feel that spanking their children is justifiable, and there are those that feel that spanking their children is never justifiable. Lest anyone think this is a trivial example, I ask that my definition above be considered again. Spanking is the deliberate infliction of harm upon another. People who do not spank their children operate upon the belief that violence can only create more violence, while people who do spank their children often seem to believe sincerely that they are raising their children to be better people. These very different belief systems tend to be reflected in other areas as well, for example, with people who do spank their children tending to be more likely to own guns and get in fights than those who do not spank their children, all justified in their own world view.
I notice that this question has been posted under the category of literature, so I have included a link on the treatment of violence in literature. But it is difficult to generalize about violence in literature. Literature is a product of its various religions, cultures, laws, and beliefs, sometimes written to reflect these, but just as likely to be written as a critique of them. In every time and place, literature can be found that justifies or argues against violence.