0

I tried to add jnativehook to my project, but when i starts jar file from console it says:

Error: Could not find or load main class com.company.Main

I work in IntelliJ IDEA, and simply created new console application and add jar to dependency. I tried to build helloworld and run it from console, and it works perfect, but when i changed programm to work with jnativehook, java started give me an Error. By the way, program works perfect in IntelliJ IDEA console, runs and all is fine.

I starts jar with this command:

java -jar consolerunner.jar

My .classpath looks ok, anyway, here it is:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
    <classpathentry kind="src" path="src"/>
    <classpathentry exported="true" kind="var" path="USER_HOME/Desktop/jnativehook-2.0.3.jar"/>
    <classpathentry kind="output" path="out/production/ConsoleRunner"/>
</classpath>

The Main, and only one class of project(Mostly, it's Keylogger example): package com.company;

import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main implements NativeKeyListener {
    public void nativeKeyPressed(NativeKeyEvent e) {
        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

        if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
            try {
                GlobalScreen.unregisterNativeHook();
            } catch (NativeHookException e1) {
                e1.printStackTrace();
            }
        }
    }

    public void nativeKeyReleased(NativeKeyEvent e) {
        System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyTyped(NativeKeyEvent e) {
        System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
    }

    public static void main(String[] args) {
        try {
            GlobalScreen.registerNativeHook();
        }
        catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());

            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(new com.company.Main());
        Logger logger = Logger.getLogger(GlobalScreen.class.getPackage().getName());
        logger.setLevel(Level.OFF);
    }
}

How can i build jar(or maybe i jast wrong starts it) to work in console?

I tried build it in ant, and no progress. Maybe, i wrong import jnahook jar, but at this point i'm too lost, i really don't know what to do. I read a lot of stackoverflow questions and couldn't find anything similar, or helpful.

UPD: Here is my manifest:

Manifest-Version: 1.0
Main-Class: com.company.Main

i know, what IntelliJ ignore .classpath, but it can use .classpath, if you set it in settings of project. And, yes, i built jar, but it doesn't works.

hoholok
  • 23
  • 1
  • 6

1 Answers1

0

You have to provide a MANIFEST.MF file. The contents of this file should be:

Main-Class: my.package.Main

Place this file in:

resources/META-INF/MANIFEST.MF