I have Java 8. An app wants Java 6 (legacy one). Can I avoid that? My government made a Java app to submit tax documents.

That app, it's actually a very outdated version of Eclipse.

When I run this ugly mess, I get a dialog that prompts me to install Java SE 6 (Legacy) from Apple.

I'd like to avoid that, for security issues, and also because I don't want to mess my Java installation

How could I force it to run on my Java 8 jvm?

The app is this: https://telematici.agenziaentrate.gov.it/Main/Desktop.jsp
You most likely can not avoid this. The app you needs to use is built to run on the Java SE 6 framework. Java 8 is not backwards compatible, so you cannot "force" this app to run on Java 8. The only way to avoid installing the Java 6 SE components is if you can somehow avoid using this particular app.

You do not want to install using the Java installer found on the Oracle website. This will result in you having multiple copies of Java on your system, and it will be a mess.

Follow the link in the notice you get, (I think it's this https://support.apple.com/kb/dl1572?locale=en_US) it will take you to an Apple download of a very specific set of "Java SE 6 Legacy runtime components". This can exist alongside your Java 8 installation and is used by certain outdated software (I used to have to install this to use an older version of Adobe Creative Suite before I updated to the CC version).

** I admit I'm not 100% sure that the SE 6 Legacy components are compatible with Mojave, but I think that they are and that there was maybe a notice that Mojave would be the last compatible version of macOS.