kasra karaji
kasra karaji

Reputation: 3

IntelliJ directories error class not found

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 ../

Upvotes: 0

Views: 139

Answers (2)

kasra karaji
kasra karaji

Reputation: 3

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.

Upvotes: 0

Dropout
Dropout

Reputation: 13866

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).

Upvotes: 1

Related Questions