Related: IntelliJ does not show 'Class' when we right click and select 'New'
In the above accepted answer, it says the "New" does not give the option for Java Class because it was not the source root (blue directory).
However, as you can see, even though where I am right-clicking is source root, indicated by blue box, it is not giving me the option for Java Class
Any idea why?
This directory is imported from git so it did not follow the usual "Create Project" for java directly from Intellij
You are right clicking in the wrong spot -- youre "too high" up the tree. Go to where you code is (as in the package under "src") and try it there and you will see the option for a New Class.
EDIT: You don't need to be in the source root. You need to be in the source directories themselves. The blue dot I believe is a git status indicator. What you want is an entirely blue folder.
Go down under "src". Likely under "src" then "main" then "java", and you're there (that's blue for me) and then you will see your package folders. Below those are where you likely want to be adding classes.
As many mentioned above you can right click on the folder you would like it in. In your case:
If you push Alt+1, it might be easier for you to right click
src > New Java Class
Alternatively you can push:
Alt+1 (Project View)
Alt+Ins
or
Ctrl+Alt+Ins
1) Right click on src
2) Click New > package
3) Name your package and press OK
4) Right click the newly made package
5) New > Java Class
If the folder where we are trying to create the class is not marked as source or test then the option is not available.
Right click the project and go to project structure then under project settings -> Modules, Select the folder where you wanted to create class and mark as Source or Test appropriately and then click apply and ok.
Now you will see the change of color of the folder/package(Sources->Blue, Test->Green) and you will get the option to create the class.
This worked for me.
I know this is an old thread, but it happened to me because my gradle files were not synchronized. So if you are within a team working on same project and face this issue, make sure to run gradle sync.
You should go inside java project then you can create java class.
Related
Not sure how to really ask this question since I don't know what everything is called but I'll try to do the best I can.
So as I was cleaning up my classes. I right clicked my project and selected refresh. Then all of a sudden my project view changed, the src folder shows everything as packages and '.' separated subpackages. And all my code package declarations seems now to be undiscoverable.
I've attached a photo, hopefully you can petter understand this...How can I fix this to have the correct directory view/structure.
Edit:
Here is what I have linked to my JAVA build path
I don't understand what is happening? How can all of a sudden my project screw up like it has? Any help will be much appreciated
Look at the 'Java Build Path' in the project 'Properties' and check that you have the correct 'Source' folders. It looks like you may have just 'src' as a source folder when you should have 'src/main/java', 'src/test/java'...
I was able to fix this by maven>update project, then just refresh...
In addition to the previous answer you also may use empty package filter:
At the top-right of the Package Explorer view, there's a downward-pointing triangle. If you hover your mouse pointer over it you'll see View Menu.
Click the View Menu, select Filters, put a check next to Empty Packages, then click OK.
Same way you could tune other options for Package explorer such as file types to show etc.
Well, it seems pretty clear that your hierarchy does not match the name of your package, so just fix your hierarchy. It will be easier if you switch to the hierarchical view of the packages. To do so, go to Package explorer, View menu (arrow icon pointing to the bottom), Package presentation, Hierarchical.
The following worked for me:
Right Click on src (which is shown like empty package in my project) -> Build Path -> Use As Source Folder
I have just downloaded the IDE, and I want to edit my first Java file with it, I'm not interested in creating a whole project, just editing the single file.
So I opened the file from my desktop with Intellij IDEA as I set it as my default program for opening .java files.
I write some code and the main run and debug buttons are greyed out! I can't run my code!
I have already installed Java 8 update 45 64-bit (I have a 64 bit OS) as well as the Java development kit (J8U45). I have set my global IDE SDK as my JDK installation, and when it prompts me I also set this as my project SDK, but still the run and debug buttons are unable to be used!
Edit: I am also unable to run my file regardless of if its in a project or not.
Edit 2: Screenshot of my project setup
Move your code inside of the src folder. Once it's there, it'll be compiled on-the-fly every time it's saved.
IntelliJ only recognizes files in specific locations as part of the project - namely, anything inside of a blue folder is specifically considered to be source code.
Also - while I can't see all of your source code - be sure that it's proper Java syntax, with a class declared the same as the file and that it has a main method (specifically public static void main(String[] args)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).
My classes contained a main() method yet I was unable to see the Run option. That option was enabled once I marked a folder containing my class files as a source folder:
Right click the folder containing your source
Select Mark Directory as → Test Source Root
Some of the classes in my folder don't have a main() method, but I still see a Run option for those.
right click on the "SRC folder", select "Mark directory as:, select "Resource Root".
Then Edit the run configuration. select Run, run, edit configuration, with the plus button add an application configuration, give it a name (could be any name), and in the main class write down the full name of the main java class for example, com.example.java.MaxValues.
you might also need to check file, project structure, project settings-project, give it a folder for the compiler output, preferably a separate folder, under the java folder,
Don't forget the String[] args in your main method. Otherwise, there's no option to run your program:
public static void main(String[] args) {
}
I had the similar issue and solved it by doing the below step.
Go to "Run" menu and "Edit configuration"
Click on add(+) icon and select Application from the list.
In configuration name your Main class: name of your main class.
Working Directory : It should point till the src folder of your project. C:\Users\name\Work\ProjectName\src
This is where I had issue and after correcting this, I could see the run option for that class.
Something else that worked for me:
Right click the folder in src containing your main
You'll see an option "run 'file.main()'" with the run icon.
Click it, and then the run icon in the top right and bottom left will turn green from then on.
Sometimes, patience is key.
I had the same problem with a java project with big node_modules / .m2 directories.
The indexing was very long so I paused it and it prevented me from using Run Configurations.
So I waited for the indexing to finish and only then I was able to run my main class.
If you can't run your correct program and you try all other answers.Click on Edit Configuration and just do following steps-:
Click on add icon and select Application from the list.
In configuration name your Main class: as your main class name.
Set working directory to your project directory.
Others: leave them default and click on apply.
Now you can run your program.enter image description here
Last resort option when nothing else seems to work: close and reopen IntelliJ.
My issue was with reverting a Git commit, which happened to change the Java SDK configured for the Project to a no longer installed version of the JDK. But fixing that back still didn't allow me to run the program. Restarting IntelliJ fixed this
-First Move Your Code Files in side the "src" Folder
-Make sure your Main method is declared like the following
public class Main {
public static void main(String []args){
}
}
then:
Go to Project configurations
select Java application,
check allow parallel run
and select your main class
and it should work
If you are just opened a new java project then create a new folder src/ in the man project location.
Then cut and paste all your package in that folder.
Then Right click on src directory and select option Mark Directory As > Sources Root.
If you use Maven, you must to declare your source and test folder in project directory.
For these, click F4 on Intellij Idea and Open Project Structure. Select your project name on the left panel and Mark As your "Source" and "Test" directory.
Is it possible to use a class from a .class file in IDEA? For example, if I have a directory with two files Ball.java and DemoBall.class, how would I use DemoBall in Ball? It says it cannot find symbol when compiling. I just want to tell IDEA that there is a compiled Java class in the directory and to recognize that, I don't want to decompile it.
EDIT:
Open IntelliJIDEA. Now Press CTL + Shift + ALT + S. This will open Project Structure.
Now Select Module from Project Settings (left hand side). And Dependencies tab from the right hand side.
Now Click on the + on the extreme right hand side toolbar. Select Jars or Directories. A new Window, Attack File or Directories will pop up.
Now browse to the location, where DemoBall.class is located. Click OK.
Select the CheckBox under Export against the location just selected. Click OK on the main Project Structure window. You are done.
Original(This one it seems is not the right way):
Open IntelliJDEA. Create a Project, with simple Java Class named Ball.java
inside it, having one main method(empty body).
Run this class, so as to create one out folder.
Copy DemoBall.class. Now through Windows Explorer/File System, go to out folder and paste DemoBall.class inside out\production\ProjectName folder.
Now simply write the code that uses this DemoBall class, inside the main method of Ball.java class.
Previously, I was wondering, as to why the red lines are coming still, even though, the program is running fine. But once, I restart, IntelliJIDEA, everythingy works fine, without showing any errors.
Here's what I'm seeing:
Normally I can choose "New Java Class" but for some reason I'm not getting this option. I created this project by opening an empty directory and building it out from there. I've seen this question, but that answer didn't help me. Here's my module settings:
Even if I open my pom.xml directly as a project, it still has this problem. How do I fix this?
The directory or one of the parent directories must be marked as Source Root (In this case, it appears in blue).
If this is not the case, right click your root source directory -> Mark As -> Source Root.
For some reason, intellij didn't recognize my directories as packages. If I created something directly from the java directory, it would give me the correct options. I fixed this by deleting everything under java/ and recreating it. This may be an intellij bug.
This happened when I was trying to add classes to a generated package using xjc plugin.
Intellij allows to add classes only to packages which are correctly names and have the "source root" status.
Can be fixed as-> right click on the directory where you're not able to add classes:
Mark sources root
refer image for clarity
This worked for me :
Right click the java folder (myproject/src/main/java)
Click "Mark Directory as"
Click "Source Root"
Right click the myproject folder (myproject/src/main/java)
Click "Mark Directory as"
Click "Source Root"
So basically had to mark some inner folder as Source project and alter mark parent project again as Source project..
Hacky way but this worked for me.
This below image is my eclipse of my two different projects. And each black rectangle is a package in folder src of Eclipse project. As you know, there a different point: The icon of each package is different.
1) first project: package has normal icon. (like java package)
2) second project: OPPS !!! It displays a folder.
And, the problem that I want to tell is: in project 1, when I type something wrong, eclipse has notice errors. For example: I declare: Example() example = new Example();
But at project 2, Eclipse cannot. That you have seen on my screen: Eclipse eclipse = new Eclipse();
Obvious, when compiled, It failed.
I think that I might do something wrong, and I just a newcomer to Eclipse, and really don't understand much this IDE.
Please help me for my problem.
thanks so much :)
As I think, because you have imported "your package" just by move your source. there are two ways (with my knowledge) that might work:
1) Like Baldrick does.
2) go to Properties\Java Build Path\Projects: add a project that contain package you want to add. this will make two project a linker. when you compile this project, this will also compile project that you link to.
Right now the second (lower) project doesn't have the source directory set up as a source directory, so it isn't trying to compile it. No compilation, no errors. Update your project settings by right-clicking and adding the source directory to the source paths.
Right click on your projects, go to "Properties" and "Java Build Path". In the "source" tab, add your folder src to the list of source folders.
Looks like you've created a New Folder in your project AndroidGame whereas you created New Package in your other project (can't see name).
You should create a new package in your AndroidGame source folder and "Refactor->Move" your source to it.
As an Eclipse newbie question, you should always Create Package (File -> New -> Package) when creating a package. Folders tend to be required for web content, images, resources (say for property files to be added in your target classpath), etc.