In upholding the ban Friday, the judge turned aside cafe owners' arguments that the law was discriminatory and would hurt business, the BBC reported.
A lawyer representing the shop owners said they would appeal.
The law will go into effect in three southern provinces next month and will be enforced across the Netherlands by the end of the year. Although certain drugs are tolerated in the Netherlands, government officials and law enforcement have expressed a concern about tourists visiting solely for drugs and about foreign dealers selling drugs illegally.
Under the new law, Dutch residents will still be allowed into the cafes as long as they present valid identification. Creation of a so-called weed pass also is being considered.
Among opponents of the ban is Michael Veling, a spokesman for the Dutch Cannabis Retailers Association.
"It is going to cost me 90 percent of my turnover," Veling said. "That is a very good reason for anyone to oppose any plan. Second it puts our customers in a very difficult spot, because why do you have to register to buy a substance that is still illegal?"
The BBC said there were about 700 marijuana cafes in the Netherlands. Cultivation and sale of drugs classified as soft through the shops have been decriminalized but remain illegal. Police generally tolerate possession of up to 5 grams of cannabis.
Shop owners indicated they would pursue their case to the European Court of Human Rights on the grounds that the Dutch government should not be allowed to discriminate against people based on where they live.