Police in Catalonia are ruling out a terrorist motive after the incident, which took place shortly after midnight at a motorway tollbooth near the town of El Jonquera.
The man, who is reported to be of Moroccan origin, was accompanied by a woman and driving a French-registered vehicle.
Finding his behaviour strange and believing he had a weapon, the police ordered him to get out.
When he did so he was still carrying the object and headed towards the officers, a source told the AFP news agency.
According to the officers, he shouted "Allahu Akbar!" (God is great!).
After asking him to stop and firing in the air, they eventually shot him in the hip, the source said, adding that he then started to undress and appeared to be suffering from mental problems.
He is now in hospital in Girona and his life is not in danger, according to police.
An investigation into the incident has been opened.