Brazil's then-presidential candidate Jair Bolsonaro grimaces after being stabbed during a campaign rally in Juiz de Fora, in southern Brazil, on September 6, 2018. [File, AFP]

A Brazilian judge on Friday acquitted a mentally ill man of attacking far-right President Jair Bolsonaro on last year's campaign trail, but jailed him for an "indefinite period."

Adelio Bispo de Oliveira stabbed Bolsonaro in the stomach on September 6 during a campaign rally in Juiz de Fora, in the southeastern state of Minas Gerais.

Police previously said Bispo acted alone in a premeditated political attack that almost cost Bolsonaro his life.

Bispo suffers a delusional disorder and for that reason cannot be tried under Brazilian law, the federal judge ruled.

However owing to the "high risk" to himself and his wish to attack Bolsonaro and former president Michel Temer, Bispo should remain in a federal prison where he can receive treatment, the judge said.

Bolsonaro said he would try to have the decision overturned.

Bolsonaro thinks that declaring Bispo mentally ill is a way to protect the attack masterminds.

"They tried to kill me. I am certain who they were, but I can't say, I don't want to prejudge anyone," he told local media.

"This is a crime against a presidential candidate who now has the mandate and we must go to the final consequences of that situation."

Bolsonaro vowed to appeal.

"I will contact my lawyer. I will try to do whatever is possible," he said.