1

I have been at this problem for a few days now, don't know why it is still happening. I have tried other solutions that have been posted on stackoverflow but no luck.

Below the following code is from the book crafting interpreters in java.

In IntelliJ 2019.4 this error always pop up when I am trying to run the .jar file.

Error: Could not find or load main class com.jam.Jam
Caused by: java.lang.ClassNotFoundException: com.jam.Jam

I have built the .jar file and setting the main class as com.jam.Jam and the class path for the .jar file as /home/username/Jam-Programming_Language/src/com/jam/Scanner.Scanner.

Directory's layout

This is the layout of my project directory.

The following solutions I have tried:

Solution 1

  1. Open Project Structure
  2. Select Modules, then click on the module for which you want the dependency
  3. Choose the Dependencies tab
  4. Click the '+' at the bottom of the page and choose the appropriate way to connect to the library file. If the jar file is already listed in Libraries, then select 'Library'.

and

Solution 2

  1. Go to the Project Structure, click on Modules, and click on your Module
  2. Choose the "Dependencies" tab
  3. Click the "+" button on the right-hand side and select "Jars or directories..."
  4. Add the directory(ies) you want (note you can multi-select) and click OK In the dialog that comes up, select "classes" and NOT "jar directory" Make sure you're using that Module in your run target

It had no effect apparently. Any clue on what I should do?

Edit: Uploaded wrong screenshot, replaced screenshot.

penguin
  • 628
  • 2
  • 10
  • 22
  • 1
    The errors on the screenshot and in the description are different. Your `Jam` file has some errors which need to be resolved before it can be compiled. What's inside `jam.jar`? Can you show the screenshot showing how this file is added to the module dependencies? Or even better, just provide a link to the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – CrazyCoder Nov 26 '19 at 03:11
  • @CrazyCoder Sorry about that, I just uploaded the new screenshot. Yes, about those errors, it seemed a bit weird when I tried compiling it. When i imported other classes, it still showed up as errors which seemed a bit unusual. I will try providing a link, never done it before on stackoverflow. – penguin Nov 26 '19 at 03:29
  • Just use any file sharing or GitHub to upload your project zipped and provide the link. – CrazyCoder Nov 26 '19 at 03:37
  • https://github.com/JamaicanFriedChicken/Intellij-Problem I uploaded the zipped project here. – penguin Nov 26 '19 at 03:43
  • 1
    Your project doesn't build because of the [errors in the Scanner class](https://i.imgur.com/EEehfJh.png). `match` and `isAlpaNumeric` methods are either not imported or not implemented. – CrazyCoder Nov 26 '19 at 03:58
  • @CrazyCoder I apologize, I have resolved those errors now (misspelled the function being invoked and didn't implement). Thanks a lot, I also had to configure IntelliJ as well. Seems as though I had to mark the `src` folder as a source root as well as adjusting the compiler used. Thank you so much! – penguin Nov 26 '19 at 04:30

2 Answers2

2

Thanks to CrazyCoder for pointing out the errors in my project (silly mistakes). It seems that after I have resolved those mistakes.

  • I had to follow the solutions I have posted then rebuild the .jar file.
  • Afterwards I need to mark the src folder as a source root folder (right click folder -> Mark Directory as -> sources root)
  • Edit Configurations if pointing to wrong main class.
  • Set Project compiler output (File -> Project Structure -> Project) to a folder designated to hold the output (any folder basically in or outside the project).
penguin
  • 628
  • 2
  • 10
  • 22
1

The error says, it is trying to run: com.jam.testfile. Your class is called: com.jam.Jam.

Click the dropdown where it shows "testfile" with a little red X on it. Select "Edit Configuration." For the "Main class", select "com.jam.Jam". That's it!

BTW, the little red X means there is some problem with your configuration.

AndyMan
  • 397
  • 1
  • 7
  • Oh, oops...I uploaded the wrong screenshot. I have tried editing the configuration putting com.jam.Jam but it still didn't change anything. – penguin Nov 26 '19 at 03:23
  • While I am starting to question the value of providing help based on screenshots, this is fun. In your new screenshot, there are red marks in the right slider. Perhaps Jam does not compile? – AndyMan Nov 26 '19 at 03:34
  • 1
    Another thing you could try is to create a new project with a main method from scratch. This is quick, and easy in IntelliJ. Update the new project to look like your old project, while verifying that it runs after each change. – AndyMan Nov 26 '19 at 03:37