Search code examples
javanetbeanscompiler-errorsprogram-entry-point

Trying to compile code - Could not find or load main class a


So I've looked up many tutorials on how to compile code. I've tried to follow all of them. But I keep getting that error. Here's my code:

package ytho;

public class a {
    public static void main(String[] args){
    System.out.println("ytho");
    }


}

And here's what I put for my manifest.txt:

Main-Class: a

I've also tried putting it as Main-Class: .a and Main-Class: ytho.a, to no avail. I'm sorry that this is a duplicate, but all other tutorials on here haven't worked for me. So, I decided to finally ask myself. Help would be greatly appreciated. And if you need more information, let me know, but I may be late to reply.


Solution

  • Lets do it step my step:

    1. Create a folder with your package name
    2. Rename your a class to A since java uses Capitals on the first letter (this is stylistic)
    3. Compile your file javac ytho/A.java
    4. Create the jar jar cvfm MyJarName.jar MANIFEST.MF *

    Here is what my directory looks like

    .
    ├── MANIFEST.MF
    ├── MyJarName.jar
    └── ytho
        ├── A.class
        └── A.java
    

    Here is the content of the Manifest

    Main-Class: ytho.A
    

    And run it like so:

    $: java -jar MyJarName.jar 
    

    which print out

    ytho