0

I'm fairly new to IntelliJ and I have tried making a hello world program in some other directory other than ~/IdeaProject. When I did so although my code is correct it gives me ClassNotFoundException. But when I copy the files into ~/IdeaProject and open the project from that directory, I can run it just fine and it isn't from my code since I tried different ones and I already uninstalled and installed IntelliJ and still got the same result anybody can tell me why? Any help is greatly appreciated!

Update: I can run my codes fine in all directories except a directory called: /Course Material/Week 2/Web Server: Reflection/ I can also run my program from command line in that directory and it works fine I suppose the problem is with IntelliJ.

 ~/U/S/I/C/W/W/untitled folder> ls out/ src/ untitled folder.iml ~/U/S/I/C/W/W/untitled folder> cd src/com/company/ ~/U/S/I/C/W/W/u/s/c/company> ls Main.java ~/U/S/I/C/W/W/u/s/c/company> javac Main.java ~/U/S/I/C/W/W/u/s/c/company> cd ../.. ~/U/S/I/C/W/W/u/src [1]> java com.company.Main write your code here 

Does anybody have any idea why?

simple program in the stated directory

same program in the relative path ../

14
  • 1
    Depends on a lot of things, for example your project structure, project configuration, etc.. Posting a picture of your project's tree might help. For example are your classes under src/main/java? Commented Mar 8, 2021 at 11:27
  • I added the screen shot to the post. and yes i only have only class which is in src/main/java Commented Mar 8, 2021 at 12:10
  • 1
    Guessing, I am wondering if that : in the path creates a problem for this or that tool. Commented Mar 8, 2021 at 12:36
  • 1
    But as said, we lack the detail to really help. See minimal reproducible example. You somehow describing what you did isnt sufficient here. Details really matter. Worst case, give a step by step description of what you tried. But then, such kind of "help" is really hard. A real person looking at your screen and being able to run this or that command might be able to figure the issue in a few seconds. But remember: we only have what you tell us about. So ... rather look around, arent there any peers or tutors that you can talk with? Commented Mar 8, 2021 at 12:37
  • 1
    Well "/" is the directory separator on Mac Os. You cant use the "/" as part of a filename. Commented Mar 8, 2021 at 12:51

2 Answers 2

1

Your project is not properly initialized and IntelliJ sees the classes as just plain files in ordinary folders and not as a project which classes interact with each other.

Start by creating a new project through File → New → Project, define your project in a way that suits your needs and after the setup is complete copy your classes under src/main/java. This is default, but basically your main classes need to be under a folder which is marked as "Sources Root". Afterwards you can also doublecheck if your SDK is configured properly (File → Project Structure → SDKs) and if you're using the correct language level in your project(File → Project Structure → Modules → Language Level).

Sign up to request clarification or add additional context in comments.

Comments

0

The problem was that I had a '/' (slash) in my file name which intelliJ replaced with ':' when I changed the slash the errors went away.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.