An Indonesian court Thursday upheld the victory of Joko Widodo at last month's presidential election, rejecting claims of widespread cheating from his opponent and ending weeks of political uncertainty in the world's third-biggest democracy.

After two weeks of hearings, the Constitutional Court said it was rejecting ex-general Prabowo Subianto's challenge to the results of the July 9 elections.

Chief Justice Hamdan Zoelva said the court was "rejecting the entire petition", after the judges spent seven hours reading out the lengthy verdict. The verdict cannot be appealed.

It clears the way for Jakarta governor Widodo, known by his nickname Jokowi and the country's first leader from outside the political and military elites, to focus on preparing for government ahead of his October 20 inauguration.

The final day of the long election season was not without drama, however -- as the judges started reading their verdict, police fired volleys of tear gas and water cannon at thousands of angry Prabowo supporters near the court.

The protesters had tried to force their way past lines of riot police and barbed wire blocking the road to the court. Three people were injured and four arrested.

Polarising election

Both Prabowo, a top military figure in the era of dictator Suharto with a chequered human rights record, and Widodo, the reform-minded governor of Jakarta, declared victory on the day of the election.

But official results released after a two-week count across the vast archipelago showed Widodo won a decisive, six-point victory after the hardest-fought, most polarising election since authoritarian rule ended in 1998.

The 53-year-old won legions of fans with his down-to-earth approach as Jakarta governor and is known by his nickname Jokowi.

But Prabowo -- who has been seeking the presidency for a decade -- refused to accept the results and his team filed a lengthy complaint against the election commission with the Constitutional Court, which has the final say on poll disputes.

His team alleged fraud occurred at tens of thousands of polling stations, and that election officials failed to order recounts in numerous places where they should have.

But evidence presented by Prabowo's team was unconvincing.

One witness claimed to be a village girl from the mountains who supported Prabowo -- only for it to emerge later she held a senior position with the ex-general's party in eastern Papua province.

Legal challenges were also mounted after Indonesia's two previous direct presidential elections, in 2004 and 2009, and both failed.

Security was tight for the announcement, with around 4,000 police on duty at the court.

Another 30,000 security personnel, including soldiers and police, were deployed around the capital, while a total of 250,000 police were on duty across the vast archipelago.

There had been concerns about the Constitutional Court's impartiality after its former chief justice was jailed in June for accepting bribes to sway his rulings in regional election disputes.

But analysts thought the court would be desperate to appear clean following the scandal.

Prabowo had previously pledged to fight on even if he loses, telling supporters this week that "our struggle has just started". But observers believe he has no other realistic options left to challenge the result.