In his first address to military graduates, Mr. Obama also pledged to invest in the men and women who defend America's liberty, not just in the weapons they take with them into battle.
"I will only send you into harm's way when it is absolutely necessary, and with the strategy, the well-defined goals, the equipment and the support that you need to get the job done," the president told more than 1,000 graduates during a sun-splashed ceremony at Navy-Marine Corps Memorial Stadium on the shores of the Chesapeake Bay.
Mr. Obama said he has halted reductions in the Navy, is building up the Marine Corps and investing in the hardware combat ships, submarines and fighter aircraft they'll need to do their jobs.
"In short, we will maintain America's military dominance and keep you the finest fighting force the world has ever seen."
He said the United States must overcome "the full spectrum of threats" in the world, ranging from the "conventional and the unconventional" and as diverse as 18th century piracy and 21st century cyberwarfare.
Mr. Obama also promised to increase support for military families.
"As you advance through the ranks and start families of your own, know too that we will be with you every step of the way, increasing your pay, increasing child care and helping families deal with the stress and separation of war," he said.
The president thanked the graduates for their service and praised them for embracing character over celebrity and country over self.
"After an era when so many institutions and individuals acted with such greed and recklessness, it is no wonder that our military remains the most trusted institution in the nation," he said. "And in a world when so many forces and voices seek to divide us, it is inspiring that this class came together and succeeded together, from every state and every corner of the world."
It was Mr. Obama's first address to military graduates, who include the son of presidential rival Sen. John McCain. The Arizona Republican was a part of the audience. Had he defeated Obama, McCain, a former naval academy graduate himself, could have been the speaker.
This was the third such speech by Mr. Obama in the past nine days. He used the previous two commencement addresses to tackle issues that threatened to overshadow both events.
At the University of Notre Dame last Sunday, abortion opponents protested the president's appearance because he supports abortion rights. He didn't avoid the debate, however, telling graduates of the country's leading Roman Catholic university that people on both sides of the abortion issue must stop demonizing one another.
The issue at Arizona State University, where the president spoke on May 13, was the school's decision not to award him an honorary degree on grounds that he hadn't accomplished enough. Mr. Obama said he agreed, saying no one's body of work is ever complete.
Presidents typically deliver a commencement address at one of the service academies each year.
Mr. Obama delivered a different kind of speech on Thursday, one in which he sought to regain control of the emotional debate over closing the detention center for suspected terrorists in Guantanamo Bay, Cuba. He denounced "fear-mongering" by political opponents and insisted that maximum-security prisons on the U.S. mainland can safely house the dangerous detainees he wants transferred out of Guantanamo.
Former Vice President Dick Cheney delivered a speech the same day denouncing some of Obama's actions since taking office as "unwise in the extreme" and repeating his contention that the new president is endangering the country by turning aside Bush-era policies.
Mr. Obama and his family were to spend the holiday weekend at the presidential retreat at Camp David in Maryland's Catoctin Mountains, traveling there on Saturday and returning Monday.