Exxon said in a statement that “both parties will now have the benefit of the certainty and finality that comes from this settlement.”

The contamination at the two sites began as early as the 1870s in Bayonne and the early 1900s in Linden, a state expert’s report has said. “Today, many of these dredge fill areas still look and smell like petroleum waste dumps,” the report noted. An environmental official, in an affidavit, cited 45 acres of “sludge lagoons,” onetime tidal marshes that had been used as hazardous-waste disposal facilities.

Much criticism of the deal, which was negotiated confidentially and first disclosed by The New York Times in February, focused on its timing. It was reached just as Judge Hogan appeared ready to rule on what damages Exxon might owe after a lengthy 2014 trial in which the state made its case for billions in compensation.

As recently as November, the Christie administration argued in a brief that “the scope of the environmental damage resulting from the discharges is as obvious as it is staggering and unprecedented in New Jersey.”

After the deal was formally announced, the state’s environmental protection agency received thousands of comments, a vast majority of them opposing the settlement, the judge noted. The administration of Mayor Bill de Blasio of New York also called for the deal to be rejected, saying it appeared “wholly inadequate” and noting its provisions made it unlikely Exxon would ever have to pay for separate claims concerning damage to tributaries and waterways of the New York-New Jersey Harbor estuary, which borders New York City and New Jersey.

Judge Hogan said in his ruling that the agreement “was entered into in good faith.” He rejected arguments that the deal was an “abrupt change of course” for the state, noting that settlement talks had taken place, unsuccessfully, as far back as 2007, under Gov. Jon S. Corzine.