How to use a limit switch to trigger a command?

Using java, how do you make it so when a limit switch is hit a command is triggered?

The answer depends on information you have not provided.

What’s hitting the switch and what command do you want to trigger?

If the command involves shutting off motor power, what motor controller are you using?

How fast must the reaction time be?

One way is to create a command which is the default command for the subsystem.
In the command’s execute, check the limit switch value and react accordingly.

Another option is to use WPILIB’s Trigger class, the documentation is a bit sparse here.

From a programming viewpoint, a limit switch is no different than a button on the controller.

If (button ==1) …
If (DIO == 1) …