1

I am trying to run some java code and have encountered a bit of an issue I cannot understand. The java class-loader simply does not "find or load" the files I'm directly pointing to.

The directory is as such

(project dir)\uwu_bot\target\classes\com\alejandro\uwu_bot\Bot.class

I make "classes" my working directory and call

java com.alejandro.uwu_bot.Bot

But I get

Error: Could not find or load main class com.alejandro.uwu_bot.Bot

I even go directly to "alejandro\uwu_bot" and do

java Bot

No dice

What could be going on?

The top answer of this question pointed me to a few Oracle articles documenting the command line tool and the process for finding class files. It didn't give me any other ideas. This post also assured me that the problem did not lie in the class name, and it was some deeper technical trouble.

The code isn't very important, and it is simply the main class to a Discord bot I manage.

Bot.java:

public class Bot extends ListenerAdapter {

    /**
     * Global instance of the main JDA
     */
    public static JDA jda;

    public static final ScheduledExecutorService SCHEDULER = Executors.newScheduledThreadPool(1);
    public static final SerialPort PORT = SerialPort.getCommPort("COM3");
    public static final ThreadLocalRandom RAND = ThreadLocalRandom.current();

    public static void main(String[] args) throws Exception {

        System.setProperty("http.agent", "Chrome");

        // Open the serial port declared in Reference
        if (!PORT.openPort())
            System.err.println("(WARNING): port busy!!");

        jda = new JDABuilder(AccountType.BOT).setToken(TOKEN).build().awaitReady();
        jda.addEventListener(new CommandListener());
        jda.addEventListener(new GeneralPurposeListener());
        jda.addEventListener(new Bot());

        ...

As told to do so by the question, I added the -Xdiag option to see further verbose output. And it says:

Error: Could not find or load main class Bot
java.lang.NoClassDefFoundError: Bot (wrong name: com/alejandro/uwu_bot/Bot)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Which states directly that it can't find the file

Then I go further out to the "classes" directory and run

java -Xdiag com.alejandro.uwu_bot.Bot

Getting

java.lang.ClassNotFoundException: com.alejandro.uwu_bot.Bot
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

Which says the class doesn't exist at all.

EDIT: This has been declared a duplicate, but I beg to differ. I specifically said I tried the solutions on that post and none worked, it's something else. I'm missing something, or that question didn't explain a specific detail I need. Keep In mind I'm not the most experienced in Java.

The file's source tree is as simple as this:

src
    ├───main
    │   └───java
    │       └───com
    │           └───alejandro
    │               └───uwu_bot
    │                   └───Bot.class
    └───test

Multiple files omitted, but these are all that matter right now

HydroPage
  • 121
  • 12
  • From which directory are you calling the command? – Seelenvirtuose Jun 08 '19 at 12:57
  • Unrelated, your package names should not contain underscores. – Michael Jun 08 '19 at 12:59
  • @Michael, why??? – Bor Laze Jun 08 '19 at 13:24
  • @Michael, 1) `The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, like com, edu, gov, mil, net, org.` - PREFIX! 2) `Subsequent components of the package name vary according to an organisation’s own internal naming conventions` - next parts of package name can be any – Bor Laze Jun 08 '19 at 13:34
  • Do you have a ```package``` statement in your class? Is your current directory in classpath? – vlumi Jun 08 '19 at 13:36
  • @BorLaze Perhaps that particular description doesn't support my argument - I must admit I didn't actually read it - but it is still certainly the convention to not use underscores, and there is plenty to be said about convention. Find me a single package in a well-known library or project that uses that form. I bet you can't. – Michael Jun 08 '19 at 13:39
  • @Michael, is it good document for you? https://docs.oracle.com/javase/tutorial/java/package/namingpkgs.html – Bor Laze Jun 08 '19 at 13:43
  • @BorLaze That says to use an underscore if the identifiers associated with a company-specific domain are illegal java identifiers which is an edge case and does not apply here: 'uwu' and 'bot' are both valid identifiers. Once again, find me a single package which actually uses that form. You won't be able to. – Michael Jun 08 '19 at 13:47
  • @Seelenvirtuose I tried about two different ones with different results. Read the question – HydroPage Jun 08 '19 at 15:26
  • @vlumi Yes, it’s packaged correctly in the file. And I tried to run it right in its directory, yes – HydroPage Jun 08 '19 at 15:28

1 Answers1

-2

You should set classpath

java -cp (project dir)\uwu_bot\target\classes com.alejandro.uwu_bot.Bot

or, if you already are in classes directory, simply

java -cp . com.alejandro.uwu_bot.Bot
Bor Laze
  • 2,458
  • 12
  • 20