"Jarheads" is a slang term for Marines that was coined by sailors during World War II. The initial intent was as an insult toward Marines, but Marines embraced the term, and it gained acceptance as a descriptor for them.
While the exact intent of the label is uncertain, it is generally believed that it stems from the fact that a Marine's head appeared to be sticking out of the top of a Mason jar because of the high collar on his dress uniform. The precursor to "jarheads" was "gyrenes," a label sailors affixed to Marines during the early 20th century. Similarly, Marines took that label in stride.