IntelliJ Idea Completion Error - java

I am new to Idea IDE. After installed it for a few days, I was fascinated with its outstanding features.
However, while I was actually writing code yesterday. I discovered that when the auto completion popped out, it showed nothing about the method of the object , merely some basic template say sync() var() ..... etc.
I have tried to setup the environment (Check the power save ...etc), but those previous solution seems to be for those whose auto-completion is turned off . Is there a way to fix it ?
THANKS IN ADVANCE!!

When you have create your project, you might not have marked it as a Java project and provide a SDK.
You can check it in File -> Project Settings -> Project -> Project SDK.
If not, you can download the JDK on the Oracle site http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
You can also try to write a
public static void main(String... args) {
System.out.println("It works !");
}
and write click on the "main" word. You should have a "Run 'Main'" option.

Related

'Could not find or load main class' in Eclipse. Again and again

I'm asking this question even though it has already been asked many times because I didn't find a solution after hours of searching.
I'm using Eclipse to learn Java. Last week everything worked properly. I could run whatever I wrote. Then today, I opened Eclipse, wrote some lines and when I tried to run it, I got an error:
Error: Could not find or load main class test.ArrayTest
Caused by: java.lang.ClassNotFoundException: test.ArrayTest
But that's not all : every project in my workspace is having the same issue. I can't run anything. At first I thought it was because I made a mistake at Eclipse start, so I closed it and re-opened it, but I was in the correct workspace.
I've tried to clean/rebuild it, doesn't work.
I've tried to mess with Properties > Java Build Path, doesn't work.
I've tried to create a new project with a simple 'Hello World', doesn't work.
package Hello;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
What I have in package explorer.
Console display.
The thing that suprises me the most is that I don't think I've done anything that can mess with Eclipse between the moment it was working and now.
Does someone have another idea ?
I think I found the root of the problem !
The path to my workspace was containing a letter with an accent ('Bibliothèque' by default with Windows 7+ in french...), so I tried to create a new workspace outside of it, and it worked !
I'm not a 100% sure it was really it, but since it worked, it's worth sharing.
Probably you have disconected JDK in project.
Try open conextual menu on project (right mouse button)
properties/Java Build Path/Libraries
check is correct JDK or remove
then: [Add Library] / JRE System Library/ Alternate JRE/ [Installed JREs] / [Add] / Standard VM / [directory]
select path to your JDK (not JRE) and confirm, select checkbox your new jdk and [Apply and close]
on select list choose your new jdk and [Finish] and [Apply and Close]
should works. I have sometimes this problems. It's Eclipse

Java Project Language Level changing on its own when I build a new project - IntelliJ

Whenever I build a new project the project language level is automatically changed on it's own down to 6. Which means that whenever I try to build the project I get errors and warnings etc. I have been trying to change the project defaults but this does not help. I have also attempted to set the byte code version as 9 by default.
This is my Project Structure before I set up a new project:
After applying these settings I create a new hello world (console) application. I then run the project and get these errors:
The only code in the main class is `public class Main {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
`
I then go and check the project structure and see that the project language level has been changed. Why is it doing that and how do I stop this from happening? I have been researching this problem and even contacted the JetBrains forum but nothing helps. + I have also attempted to set the 'project byte version' as 9 by default but this also gets changed after I create a new project.
This is what project structure looks like after running the project:
During my research I found out that if this is a maven project you can fix this by changing the pom.xml file but this is just a java console application.
These are the java compiler settings:
FYI by default it is set to 9; when the project is created it is set to nothing.
Try change the same settings using File -> Other Settings-> Default Settings

Error: Could not find or load main class in intelliJ IDE

I'm a beginner in Java and am trying to run my code using IntelliJ that I just installed as my IDE with JDK 1.7. The following piece of code keeps does not even compile and keeps giving me the error:
Error: Could not find or load main class libTest
Code
import java.lang.Integer;
import java.lang.String;
import java.lang.System;
import java.util.*;
class book {
private String name = "trial";
private int bookCode=1;
private int issued=0;
public void Issue(){
if(issued==0) {
issued=1;
System.out.println("You have succesfully issued the book");
}
else {
System.out.println("The book is already issued. Please contact the librarian for further details");
}
}
public int checkCode() {
return bookCode;
}
String readName() {
return name;
}
public void setName(String newName){
name=newName;
}
public void setBookCode(int newCode){
bookCode=newCode;
}
}
class library {
private ArrayList books=new ArrayList();
public void getList(){
for(int bk:books){
String bName=books(bk).readName();
System.out.println((bk+1)+") "+bName);
}
}
}
public class libTest{
public static void main(String[] args){
library newLib= new library();
System.out.println("code working");
}
}
Is there any change that i have to make in the compiler settings?? Or is it the code.
This might help:
1) "Build" menu -> "Rebuild Project".
Sometimes Intellij doesn't rewrite the classes because they already exist, this way you ask Intellij to rewrite everything.
2) "Run" menu -> "Edit configuration" -> delete the profile -> add back the profile ("Application" if it's a Java application), choose your main class from the "Main Class" dropdown menu.
3)"Build" menu -> "Rebuild Project".
If none of the above answers worked for you, just close your IntelliJ IDE and remove the IntelliJ IDE file and folder from the root of your project:
rm -rf .idea *.iml
Then open the project with IntelliJ. It must work now.
For me the solution was to fix the output directory under project settings. Before I was using just "target" for the Project compiler output. Instead I updated it to have a full path e.g. D:\dev\sigplusjava2_68\target
I had this problem and I tried everything under the sun that I could think of and on this site.
None of my Java classes were being picked up after I pulled from a remote branch. All the classes had red Js by their names in the Project Hierarchy, not blue Cs.
In the end, I tried to follow this tutorial and a few steps in tried something not described and fixed the issue:
https://www.jetbrains.com/help/idea/creating-and-managing-modules.html
Here's what I did:
Goto File | Project Structure, or press Crtl+Shift+Alt+S
Select Modules under the Project Settings section.
In the Sources tab click Sources on the 'Mark as:' line.
Click the Apply button.
For some reason, all my classes then had blue C's.
Someone with a better understanding of how IntelliJ and/or IDE's might be able to explain the phenomenon, but all I know is now it can see all the classes and more importantly the main one, and run.
Invalidate cache and restart your IntelliJ, it worked for me.
Explicitly creating an out folder and then setting the output path to C:\Users\USERNAME\IdeaProjects\PROJECTNAME\out
seemed to work for me when just out, and expecting IntelliJ to make the folder wouldn't.
Also try having IntelliJ make you a new run configuration:
Find the previous one by clicking
then remove it
and hit okay.
Now, (IMPORTANT STEP) open the class containing your main method. This is probably easiest done by clicking on the class name in the left-hand side Project Pane.
Give 'er a Alt + Shift + F10 and you should get a
Now hit Enter!!
Tadah??
(Did it work?)
File > Project Structure > Modules > Mark "src" folder as sources.
This should fix the problem. Also check latest language is selected so that you don't have to change code or do any config changes.
I know this was asked a while ago, but I was just stumbling over this issue and thought my findings might help others. As pointed out, the error message is basically a result of the out folder. That's because, when you're trying to run the program, it compiles the code first, and puts the compiled result to the out location, and then it tries to load the compiled code from the out location. If the compiled code is not in the location expected, you'll get the error.
The point I'm particularly wanting to share is that some times, the code is not compiled (built), even though your run configuration specifies "Build" in the "Before launch" section of the configuration panel.
When can this happen?
One situation that can cause this to happen is if you're using modules and you manually delete the module out directory. For example, if I have a module named "foo", there should be a directory named foo under out/production. If you manually delete it, the build system may not know that it needs to be rebuilt.
Even worse, if you select Build | Build module 'foo', it still may not rebuild the module. If that's the case, you should select a file in the module, for example 'bar.java' and then select Build | Recompile 'bar.java'. Now the out directory out/production/foo should be restored.
Since IntelliJ typically knows about any changes going on, this surprised me, and took me a little time to figure out, so I thought I'd share.
Check your class module : I have encountered this problem with intellij :
I have a maven multi-module project, the problem is that i runing a class which not exist the module within the configuration, so my problem is fixed by setting the right module ("edit configuration" -> "use class of module")
may this help you
I had to mark the "src" folder as "Sources". After restarting IntelliJ and rebuilding the project I could run the project without further issues (see screenshot).
Edit: You can access the "Project Structure" tab via File->Project Structure or by pressing Ctrl+Shift+Alt+S.
I ran into this problem when my Java class was under src/main/kotlin. After I moved it to src/main/java, the problem was gone.
I have faced such problems when the class is in the default folder, i.e. when the class does not declare a package.
So I guess using a package statement (eg. package org.me.mypackage;) on top of the class should fix it.
Open Modules Tab (Press Ctrl+Shift+Alt+S). I had two modules under one project. I've solved the problem after removing the second redundant module (see screenshot).
After creating your project in intelliJ, try running the following command:
mvn package
I have tried all the hacks suggested here - to no avail. At the end I have simply created a new Maven application and manually copied into it - one by one - the pom.xml and the java files and resources. It all works now. I am new to IntelliJ and totally unimpressed but how easy it is to get it into an unstable state.
Invalidating cache didn't work.
I edited the main class java file with a dummy change and ran it. It worked.
In my case the problem seemed to be related to upgrading IntelliJ. When I did this I overwrote the files from the old IntelliJ with the files from the new IntelliJ (2017 community to 2018 community). After that all of my projects were broken. I tried everything in this thread and none of them worked. I tried upgrading gradle to the latest version (4 to 4.8) and that didn't work. The only thing that worked for me was deleting the entire IntelliJ folder and reinstalling it. All of my projects worked after that.
I have tried almost everything suggested in the answers here, but nothing worked for me.
After an hour of just trying to run my application, I noticed that my project's path included non-ASCII characters (Arabic characters). After I moved my project to a path with no non-ASCII characters, it executed just fine.
Goto File-> Invalidate Caches and Restart .
Else delete rm -rf .idea *.iml
and restart InteliJ
You can run the maven command on the pom.xml file in your project directory:
mvn clean install
For me - i tried few of the options above, did not work. Then i just renamed my Application class and that probably forced intelliJ to build a fresh jar and error message started to change. Then i renamed it back and it worked.
Mark the directory as a source directory. Opened via Ctrl+Shift+Alt+S
modules.xml with wrong content, I don't know what's matter with my IDEA.
I inherited a bunch of .JAVA files from elsewhere and couldn't figure out how to get them to work in any IDE. Ultimately I had to go to the command line where the Main.JAVA file was and run javac Main.java. This created a bunch of .CLASS files. The IDE's were then able to figure out what to do.
I got this error when using Scala/SBT. IntelliJ could not find the main class, even though everything was set up correctly.
My solution: delete the <user>/.sbt/<version>/plugins/target folder, then restart IntelliJ.
You probably would have specified a wrong package and the package hierarchy would not be right. Look below
The ide would highlight the wrong path in that case.
I'm using IntelliJ with Spring and my main class is wrapped in a JAR.
I had to mark the 'Include dependencies with "Provided" scope' in the Run/Debug configuration dialog
We are at File/Project Structure..
Answer might be:
Folder indicated as "content root" needs a child folder where the code is.
Plus find the button that marks code as excluded and not.
Not to be confused with tickbox that states excluded without telling in what phase and what**
Is it compiler exclude or runtime exclude? You are doomed to test and lot.
So no that tickbox but icons and colors.
As an idea we need to crack how it was originally thought to work. They never got it to work in first place and started add things in premature codaculation style.
It has been so many years and you cannot expect any improvement.
But as cure we can hack out some way to get it right every time.
Another thing you can check here is the actual command that is being passed to the JVM and make sure it looks OK. Scroll to the top of your Run console, it should be the first line.
Spaces in your Run Configuration VM Options field will malform the app startup command and can result in this error message
-DsomeArgument="arg with space must be quoted"
I am working with Kotlin but am guessing the problem is the same. I would start a project, create a single file and add main to it and the IDE couldn't find the main.
I tried the things in this list and none worked. I finally mentioned my frustration on one of the IntelliJ pages and was contacted. Of course, it worked fine for IntelliJ. After a couple of days back and forth, I noticed that the highlight function wasn't working and mentioned that. It turned out something was wrong with the IDE settings. I still don't know specifically what was wrong but the fix in my case was to reset the IDE settings. File->Manage IDE Settings->Restore Default settings.
After this, the green triangle start icon became visible to the left of my main function and things continued to work normally for subsequent projects.
Thanks to Konstantin at JetBrain's support for his patience.

Eclipse returns error on run: "java selection does not contain a main type"

I am unable to run the following code in Eclipse (Eclipse IDE for Java Developers, Version: Indigo Service Release 1, Build id: 20110916-0149), and I think that I may have a configuration problem in Eclipse (but I do not know what or where):
class Saluton {
public static void main(String args[])
{
String greeting = "Saluton mondo!";
System.out.println(greeting);
}
}
I am also running against Java 6 (1.6.0_29-b11-402) on Macintosh 10.7.2.
When I run this, I get the error:
"java selection does not contain a main type"
but I am pretty sure that my class is written correctly...? I have looked this error up, and cannot explain this problem so far.
TIA for any thoughts or opinions!
I think you have to make your class public, otherwise it won't work?
EDIT: My previous answer was incorrect. The JLS says you can declare arrays with the brackets at either end. See here for some examples. My mistake.
Have you verified your configuration in Eclipse such that it knows where to find the Java compiler, and a JVM? You can check the project-specific libraries by right clicking the project and going to Properties, Java Build Path
You can also check the JRE's installed by clicking Window, Preferences, Java, Installed JREs. Make sure the JRE you wish to use is listed here and that the path is correct.
Another solution (simple and direct):
In Eclipse: File -> Restart
Right click your class Saluton and choose Run as --> Java Application.
It should work.
Ok, so I'm a fellow noob, and I came here because I had this problem. I checked all my class paths and everything, and they were correct. I had actually been putting all of my files in the JRE System Library Folder, instead of the source folder. I'm not sure if this was you'r problem, but it seemed to work for me when I moved the code files into the src folder.
I took the long way around to solve this; eventually I created a package within my project and added my source code to that package and it compiled and ran! I am learning that Wizards rule!
It will work if you make your class as public !

Fatal error by Java runtime environment

I am executing a junit test case
I got the following error,
A fatal error has been detected by the Java Runtime Environment:
Internal Error (classFileParser.cpp:3174), pid=2680, tid=2688
Error: ShouldNotReachHere()
JRE version: 6.0_18-b07
Java VM: Java HotSpot(TM) Client VM (16.0-b13 mixed mode windows-x86 )
Can any body please suggest the solution to resolve
I got the same problem, but with alot of googling I found the answer! See this page
Quote from the link:
# An unexpected error has been detected by Java Runtime Environment:
#
# Internal Error (classFileParser.cpp:2924), pid=5364, tid=6644
# Error: ShouldNotReachHere
That's because we are using Android's JUnit stub implementation. Go to Run -> Run As -> Run configurations again and in the recently created JUnit configuration Classpath's Bootstrap Entries remove Android Library
Then Add Library, using Advanced... button, and add JRE System Library and JUnit 3
Apply and Run
Try this, it worked for me.
You'll need to take this up with Sun -- looks like a JVM bug to me. If it's reproducible, you should be able to run java in such a way as to generate more details (e.g. -verbose, etc). If you can reduce it to a minimal case that triggers the bug (source code always helps!), that also goes a very long way.
http://java.sun.com/developer/technicalArticles/bugreport_howto/index.html
http://bugreport.sun.com/bugreport/crash.jsp
In the meantime, you might want to try it with a different JVM implementation (maybe even an older patch level of the Sun JRE).
Go to Run As -> Run Configurations... and select the configuration you are using.
Select the Class Path tab and select BootStrap Entries.
Click on Advance, then Add Library and select JRE System Library.
Bring it up and make it the first entry in the BootstrapEntries List.
Apply and Run...
Another possible explanation: hardware failure. Ruled out if you can reproduce the error on different machines.
I resolved this by
Quit eclipse
Delete the bin and gen directories in your project
Start eclipse
Rebuild your project
I just recently found solution for this issue that was posted by devdanke:
"As of 11-July-2010 and Android 2.1, the work around I use is to segregate tests into different classes. Any test(s) that don't call any Android APIs go into their own classes. For each of these classes, I remove the reference to Android in their Run Configurations, Classpath tab."
The problem with having it configured class by class is then is not possible to run all tests in project. Better approach is creating 2 test projects with different sets of libraries.
Standard Android JUnit Test project can be created following link, and sample test class looks like:
import android.test.AndroidTestCase;
public class ConverterTest extends AndroidTestCase {
public void testConvert() {
assertEquals("one", "one");
}
}
Then JUnit Test project can be converted from Android JUnit Test project by removing Android Library from project build path, and adding JRE System Library, and JUnit 3 library, and sample test class looks like:
import junit.framework.TestCase;
public class ConverterTest extends TestCase{
public void testConvert() {
assertEquals("one", "one");
}
}
I have had a similar problem, I found it was because I had generated a new activity with a main[] stub entry. Once I deleted the main[] code from the new activity templatye the error went away.
YMMV
This could be a JVM bug; see #Zac's answer. But it could also be that your junit test case is causing a corrupted bytecode file to be loaded. Try rebuilding all your .class files, and if that does not fix the problem try refetching any external libraries that your code depends on.
Do you run on a supported platform (Windows, one of a few Linux versions?) If not, that is the first to try.
If you ARE on a supported platform, then downgrade to _17 and see if THAT helps.
Then make a bug report to Sun and hope they will fix it someday (unless you want to give them money for fixing it faster).
Go to Run As -> Run Configurations->classpath->BootStrap Entries
Click on Advance, then Add Library and select JRE System Library as a first entry.
Apply and Run...
I am not sure whether you were able to reach the solution for your problem or not but your question just popped up while I was searching for the solution for the same problem I am facing. And I got one solution from the stack itself, so just thought to share a link with you if that aids you by any means. The link is as below:
Can't run JUnit 4 test case in Eclipse Android project
Another possible reason (for future references):
I had accidentally copied in a main method in my code, causing Eclipse to recognize the project as a java application, thus launching it with that configuration.
To solve it I went into Run > Run Configurations... and then changed from my presumed main in java application to the main activity of my android application simply by choosing it in the left column.

Categories

Resources