简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM