A javelin can be gained in many different ways. The most common way is charging an auto-javelin. This occurs near the end of a level, when less bricks are present. The less bricks, the faster it charges. When there is one brick left, it chages in less than a second! Another way to gain a javelin is to pick up a J powerup. This will charge the Javelin meter (black/yellow line at the bottom of the screen) to the max, giving you a Javelin instantly.
Your ship is not the only thing capable of firing a Javelin. Dragon Bricks (like the ones on Gorgonat) will fire horizontal Javelins, depending on the direction they are facing. This will then deplete the brick, which can then be broken with enough damage.