Presidential candidate Prabowo Subianto (2L), with running mate Hatta Rajasa (L) and candidate Joko Widodo (2R), with running mate Jusuf Kalla (R) and moderator sing the national anthem during the opening of the final live nationwide television debate from Jakarta on Saturday. Agence France-Presse/Getty Images

JAKARTA, Indonesia—The two men vying for the leadership of Indonesia spoke directly to the nation for the last time before the election next week, taking a number of rare jibes as they tried to sway millions of undecided voters in a tightly contested election.

Joko Widodo, governor of the megacity of Jakarta, and former army general Prabowo Subianto participated in the final debate alongside their running mates for more than two hours on Saturday night.

This debate had a different air than prior encounters, which were mainly polite weekly run-ins over the past month.

About 190 million voters are eligible to cast ballots in the country's third direct presidential election since autocrat Suharto was ousted from power in 1998. Up to 10% of those voters remain undecided, according to various pollsters. Many are farmers, a group that figured heavily into the answers of both men throughout the night.

The candidates debated food and energy security in a country where a growing consumer class and waning oil and gas exploration has strained supplies of both. Previous debates didn't include as much direct confrontation between candidates.

Mr. Subianto, known for both his charm and a volatile temperament, appeared to lose his composure at times, raising his voice when Mr. Widodo asked him to explain what he had accomplished as head of a farmers welfare organization.

For his part, Mr. Widodo, who looked more comfortable wearing his usual checkered shirt instead of the suit he wore during the other debates, appeared unphased, such as when Mr. Subianto suggested the governor had spoken against creating farmer cooperatives, all important for marginalized Indonesians with no access to formal banks.

"Perhaps Mr. Prabowo misread or misheard," said Mr. Widodo. "Everyone knows that cooperatives are the foundation of our economy. It is impossible that Jokowi doesn't know that," he said, referring to himself by nickname.

Mr. Subianto also pushed Mr. Widodo to answer whether he agreed with a goal of opening up millions of acres of farmland in a country where half the population live in rural areas.

Mr. Widodo, answered affirmative and then seemed to take a jibe at the big-picture nature of Mr. Subianto's normal rhetorical style: "How to get there, how to implement it, that's what's important."

Analysts said the contentious nature of the debate reflected both men's need to turn the corner among undecided voters, who include large numbers of farmers.

"This was the last debate, and they were trying to boost the votes," said Dodi Ambardi, executive director of pollster Lembaga Survei Indonesia.

Mr. Widodo appeared to succeed on that measure, appealing directly to farmers, who number in the tens of millions, stressing the help they need in creating markets for exports.

"Don't underestimate farmers. As long as they're given the guidance, a mentor, given the right seed, they can plant anything," he said. "The problem is that we have never prepared the markets" or the "post-harvest industries."

True to form, Mr. Subianto spoke in larger terms about the nation of 250 million people as a whole. He stressed the need to open up new agricultural lands and improve irrigation by building more reservoirs. He said using different fertilizers could boost agricultural output by 40%.

Neither side appeared to outscore the other on the topic of energy.

Presidential candidates Joko Widodo, left and Prabowo Subianto participate in the final live debate on Saturday. Agence France-Presse/Getty Images

Indonesia, once a member of Organization of the Petroleum Exporting Countries, today imports oil and allocates billions of dollars in subsidies to sell it to the public at cheap prices that increasingly threaten to break the budget. Deep-sea exploration for new reserves is beyond the country's technology, and alternative energies such as geothermal, in which the volcanic islands offer great potential, have yet to be developed in earnest.

Both teams stressed the need for energy diversification and bringing alternative fuels into the mix. Analysts said the answer was unlikely to matter much with undecided voters.

Mr. Widodo offered a complex explanation of the country's energy problems, bringing up the need to build gas pipes to ease in major cities lacking efficient public-transport systems, and the need to open the biofuels market and offer incentives for farmers to plant such degraded-lands crops as sorghum.

If past debates are any indication, Sunday's media will declare winners and losers in the debate. But on the whole, analysts said each pairing likely appealed to their huge bases.

"The content was good," said Husin Yazid, a local pollster. "They seemed to know what they were talking about."

Write to Ben Otto at ben.otto@wsj.com, Andreas Ismar at andreasismar.sandiwan@wsj.com and I Made Sentana at i-made.sentana@wsj.com