Gauge-invariant on-shellZ2 in QED, QCD and the effective field theory of a static quark