How to fix Java error "Unresolved compilation problem" in Eclipse IDE? - java

I'm trying to dabble with a basic ComboBox (i hear these are very outdated). The goal is to create a box with options for the user and when they click said option in the ComboBox they will get information from an array of strings based on the case detected by the switch. This is my code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SolicitorComBox extends JFrame implements ActionListener {
String[] MuniArray = {"Allepo", "Avalon"};
JComboBox MuniList = new JComboBox (MuniArray);
JLabel lblText = new JLabel();
public static void main (String[] args) {
SolicitorComBox fr = new SolicitorComBox();
centerFrame(fr);
fr.setVisible(true);
}
private static void centerFrame(SolicitorComBox fr) {
// TODO Auto-generated method stub
}
public SolicitorComBox() {
setLayout (new FlowLayout());
setSize (400, 300);
setTitle ("Solicitor Search");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MuniList.setSelectedIndex(0);
MuniList.addActionListener(this);
add(MuniList);
add(lblText);
}
public void actionPerformed(ActionEvent e) {
if (e.getsource() == MuniList) {
JComboBox cb = (JComboBox).e.getsource();
String msg = (String)cb.getSelectedItem();
switch (msg) {
case "Allepo": lblText.setText("The attorney is Joe!");
break;
case "Avalon": lblText.setText("The attorney is Dana!");
break;
This is the error I'm getting when I try to run the code:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at SolicitorComBox.main(SolicitorCombobox.java:10)

beside the fact you didn't post your whole code, I was able to run the piece of code you put over there:
So or your issue is below the code you posted, or it is just a compilation problem in your IDE
I would recommend to you manually refresh your project and force clean it using the menu Project > Clean in Eclipse. I will force your app to recompile.

I had a similar problem. I was using an imported gradle project but running it from inside eclipse. For some reason the error suddenly started to appear.
Solution 1. In eclipse click clean then refresh. This worked, but not for very long
Solution 2. Gradle has an eclipse task for interacting with the eclipse environment and making sure its libs are correct: So running gradle clean build eclipse seemed to provide a solution. Presumably maven has a similar task.

you may be missing some jar files. Right click on your project or class, goto build path and select "build path configuration". from the "build path" window, select "libraries" tab, find the missing files, download them and put the in your project from the "project explorer" side. you can use drag and drop.
Now click on "Add JARs" to add the jar files to the library from the project.
it worked for me. hope it works for you.

To fix Java error “Unresolved compilation problem” in Eclipse IDE follow these steps :
Try to include package name in eclipse in case if you forgot it
Import all packages before using it, EX: import java.util.Scanner before using Scanner class.
Also make sure to check compiler compliance level and selected jdk version is same
I eclipse, if you skip including package it will give "Unresolved compilation problem"

Related

im trying to create a thread that sends and receives a message in java however im unsure on how to solve the error messages that are displayed [duplicate]

I have been working on an assignment for my class in programming. I am working with NetBeans. I finished my project and it worked fine. I am getting a message that says "No main class found" when I try to run it. Here is some of the code with the main:
package luisrp3;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class LuisRp3 {
public static void main(String[] args) throws FileNotFoundException {
java.io.File newFile = new java.io.File("LuisRamosp4.txt");
if (newFile.exists()) {
newFile.delete();
}
System.setOut(new PrintStream(newFile));
Guitar guitar = new Guitar();
I posted this before but had a couple issues. i have fixed the others and now have just this one remaining. Any advice will be greatly appreciated.
Right click on your Project in the project explorer
Click on properties
Click on Run
Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass)
Click OK.
Run Project :)
If you just want to run the file, right click on the class from the package explorer, and click Run File, or (Alt + R, F), or (Shift + F6)
Also, for others out there with a slightly different problem where Netbeans will not find the class when you want when doing a browse from "main classes dialog window".
It could be that your main method does have the proper signature. In my case I forgot the args.
example:
public static void main(String[] args)
The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above.
Args: You can name the argument anything you want, but most programmers choose "args" or "argv".
Read more here:
http://docs.oracle.com/javase/tutorial/getStarted/application/
When creating a new project - Maven - Java application in Netbeans
the IDE is not recognizing the Main class on 1st class entry. (in Step 8 below we see no classes).
When first a generic class is created and then the Main class is created Netbeans is registering the Main class and the app could be run and debugged.
Steps that worked for me:
Create new project - Maven - Java application
(project created: mytest; package created: com.me.test)
Right-click package: com.me.test
New > Java Class > Named it 'Whatever' you want
Right-click package: com.me.test
New > Java Main Class > named it: 'Main' (must be 'Main')
Right click on Project mytest
Click on Properties
Click on Run > next to 'Main Class' text box: > Browse
You should see: com.me.test.Main
Select it and click "Select Main Class"
Hope this works for others as well.
The connections I made in preparing this for posting really cleared it up for me, once and for all. It's not completely obvious what goes in the Main Class: box until you see the connections. (Note that the class containing the main method need not necessarily be named Main but the main method can have no other name.)
I had the same problem in Eclipse, so maybe what I did to resolve it can help you.
In the project properties I had to set the launch configurations to the file that contains the main-method (I don't know why it wasn't set to the right file automatically).
In project properties, under the run tab, specify your main class.
Moreover, To avoid this issue, you need to check "Create main class" during creating new project. Specifying main class in properties should always work, but if in some rare case it doesn't work, then the issue could be resolved by re-creating the project and not forgetting to check "Create main class" if it is unchecked.
If the advice to add the closing braces work, I suggest adding indentation to your code so every closing brace is on a spaced separately, i.e.:
public class LuisRp3 {
public static void main(String[] args) throws FileNotFoundException {
// stuff
}
}
This just helps with readability.
If, on the other hand, you just forgot to copy the closing braces in your code, or the above suggestion doesn't work: open up the configuration and see if you can manually set the main class. I'm afraid I haven't used NetBeans much, so I can't help you with where that option is. My best guess is under "Run Configuration", or something like that.
Edit: See peeskillet's answer if adding closing braces doesn't work.
There could be a couple of things going wrong in this situation (assuming that you had code after your example and didn't just leave your code unbracketed).
First off, if you are running your entire project and not just the current file, make sure your project is the main project and the main class of the project is set to the correct file.
Otherwise, I have seen classmates with their code being fine but they still had this same problem. Sometimes, in Netbeans, a simple fix is to:
Copy your current code (or back it up in a different location)
Delete your current file
Create a new main class in your project (you can name it the old one)
Paste your code back in
If this doesn't work then try to clear the Netbeans cache, and if all else fails, then just do a clean un-installation and re-installation of Netbeans.
In the toolbar search for press the arrow and select Customize...
It will open project properties.In the categories select RUN.
Look for Main Class.
Clear all the Main Class character and type your class name.
Click on OK.
And run again.
The problem is solved.
If that is all your code, you forgot to close the main method.
Everything else looks good to me.
public class LuisRp3 {
public static void main(String[] args) throws FileNotFoundException {
java.io.File newFile = new java.io.File("LuisRamosp4.txt");
if (newFile.exists()) {
newFile.delete();
}
System.setOut(new PrintStream(newFile));
Guitar guitar = new Guitar();
}}
Try that.
You need to add }} to the end of your code.
You need to rename your main class to Main, it cannot be anything else.
It does not matter how many files as packages and classes you create, you must name your main class Main.
That's all.
import java.util.Scanner;
public class FarenheitToCelsius{
public static void main(String[]args){
Scanner input= new Scanner(System.in);
System.out.println("Enter Degree in Farenheit:");
double Farenheit=input.nextDouble();
//convert farenheit to celsius
double celsuis=(5.0/9)*(farenheit 32);
system.out.println("Farenheit"+farenheit+"is"+celsius+"in celsius")
{
I also experienced Netbeans complaining to me about "No main classes found". The issue was on a project I knew worked in the past, but failed when I tried it on another pc.
My specific failure reasons probably differ from the OP, but I'll still share what I learnt on the debugging journey, in-case these insights help anybody figure out their own unique issues relating to this topic.
What I learnt is that upon starting NetBeans, it should perform a step called "Scanning projects..."
Prior to this phase, you should notice that any .java file you have with a main() method within it will show up in the 'Projects' pane with its icon looking like this (no arrow):
After this scanning phase finishes, if a main() method was discovered within the file, that file's icon will change to this (with arrow):
So on my system, it appeared this "Scanning projects..." step was failing, and instead would be stuck on an "Opening Projects" step.
I also noticed a little red icon in the bottom-right corner which hinted at the issue ailing me:
Unexpected Exception
java.lang.ExceptionInInitializerError
Clicking on that link showed me more details of the error:
java.security.NoSuchAlgorithmException: MD5 MessageDigest not available
at sun.security.jca.GetInstance.getInstance(GetInstance.java:159)
at java.security.Security.getImpl(Security.java:695)
at java.security.MessageDigest.getInstance(MessageDigest.java:167)
at org.apache.lucene.store.FSDirectory.<clinit>(FSDirectory.java:113)
Caused: java.lang.RuntimeException
at org.apache.lucene.store.FSDirectory.<clinit>(FSDirectory.java:115)
Caused: java.lang.ExceptionInInitializerError
at org.netbeans.modules.parsing.lucene.LuceneIndex$DirCache.createFSDirectory(LuceneIndex.java:839)
That mention of "java.security" reminded me that I had fiddled with this machine's "java.security" file (to be specific, I was performing Salvador Valencia's steps from this thread, but did it incorrectly and broke "java.security" in the process :))
Once I repaired the damage I caused to my "java.security" file, NetBeans' "Scanning projects..." step started to work again, the little green arrows appeared on my files once more and I no longer got that "No main classes found" issue.
Had the same problem after opening a project that I had downloaded in NetBeans.
What worked for me is to right-click on the project in the Projects pane, then selecting Clean and Build from the drop-down menu.
After doing that I ran the project and it worked.
Make sure the access modifier is public and not private. I keep having this problem and always that's my issue.
public static void main(String[] args)

"Could not find or load main class" while trying to use JavaFX in Eclipse

java version 1.8.0_221
eclipse version 2019-06 (4.12.0)
I'm trying to make a basic JavaFX program. I've imported jfxrt external jar into the libraries tab under module build path.
I've been getting this error "Error: Could not find or load main class firstFX.eighthTry
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application"
The only advice I've found around the web tells me to get rid of any external jars, but I need jfxswt in order to use JavaFX. I've also tried to use Project > Clean, but that hasn't done anything either.
I'm at a loss, is there anything I can do to fix this? Code is below
package firstFX;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class eighthTry extends Application{
public static void main(String[] args) {
// TODO Auto-generated method stub
launch(args);
}
#Override
public void start(Stage arg0){
// TODO Auto-generated method stub
arg0.setTitle("Hello World!");
final Button btn = new Button();
btn.setText("Click Me!");
final StackPane root = new StackPane();
root.getChildren().add(btn);
arg0.setScene(new Scene(root, 300, 250));
arg0.show();
}
}
Edit:
I've done a clean install of Eclipse (now 2019-09) and also installed e(fx)clipse via Help>Install new software then installing both things from http://download.eclipse.org/efxclipse/updates-released/3.3.0/site. I followed this tutorial.
For my code I added the JavaFX SDK under Properties> Java Build Path > Libraries>Module Path. I then put it at the top of the Order and Export tab.
Unfortunately, this hasn't changed anything. I still get the same error as I did above.
Additionally, I was mistaken about the name of the external jar I needed. I have since replaced the jfxswt.jar with jfxrt.jar. However, this has not fixed the error.
Not sure how it looks but your class is missing package firstFX; is it in a folder called firstFX... ?

Debugging java source file with multiple classes in eclipse: Source not found

I have similar problem to the one described here:
Eclipse and Java - source not found
I also looked at the following question: Eclipse java debugging: source not found but I could not see how that it applied to my case..
I have just started using Eclipse and its debugger.
Here is how to reproduce the problem using Eclipse 3.7.2 on Ubuntu 12.04 with java and javac version 7.
Start Eclipse and select workspace, e.g., "Test" in home folder.
Open java perspective
Open new java project with project name "Test"
Add a new java class "Test"
I now have the following screenshot:
Add the following code to the source file Test.java
set a breakpoint at new Test2(1)
open debug perspective
start debugging:
choose Step Into (F5)
Now the error is reported:
Any help on this issue is appreciated..
The class Launcher$AppClassLoader belongs to the JRE and is about to load your class. It has nothing to do with the source code of your own classes. If you step further you will reach your own class Test2. If you go to the end of your debug button bar (four buttons right to the “step into” button), there’s a “Use step filters” button. Activate it to avoid unnecessary steps into the JRE classes.
I believe you have to create an instance of Test before you can access the nested class Test2 in Test. Eclipse should have thrown an error in yours saying something like "No instance of Test2 is accessible" or something like that. Change your code to look like this and see if it works.
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test mTest = new Test();
Test2 nTest = mTest.new Test2(1);
}
class Test2{
int i;
Test2(int i){
this.i = i;
}
}
}

I am using eclipse and i get an error--I think it's an error with the way i imported the becker file [duplicate]

This question already has answers here:
I am using eclipse and i get this error--I think it's an error with the way i imported becker.robots
(4 answers)
Closed 9 years ago.
I am using eclipse and i get this error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at Wall.main(Wall.java:6)
her's a screen shot"http://postimg.org/image/ufvv9p6aj/"
Here is the code:
import becker.robots.*;
import javax.swing.*;
public class Wall
{
public static void main (String[] args)
{
JFrame frame = new JFrame ();
frame.setVisible(true);
JPanel panel = new JPanel ();
panel.setVisible(true);
frame.add(panel);
JColorChooser color = new JColorChooser();
panel.add(color);
}
}
The error means you are trying to use s method which doesn't compile. Turn this feature off and look at the compilation error.
you don't appear to be using the becker library so it doesn't matter if you had incorrectly imported it.
Duplicating my answer from the duplicate post:
Your problem is that becker.jar is just listed in your source folder (it is not source, it is a compiled jar and therefore should be in a lib folder, and then added to the Build Path). Eclipse can't find your import becker.robots.* because it doesn't know about becker.jar. Move becker.jar to /lib/becker.jar, and then Right Click on your Project Folder > Properties > Java Build Path > Libraries Tab > Add Jar > (select your lib/becker.jar). That should resolve your compilation error.
A side note - the reason the error shows up as line 6 is because that is where the main method is (this is what actually is being run when you try to run the application), and when attempting to run the main method it identifies the build error that is really on line 1

"Selection does not contain any Java files" from eclipse

Below my Test.java which is one .java file from a student project. I don't see why eclipse gives me
" Selection does not contain any Java files " error when I tried to "run" it from the menu Run-->Run ? Could you explain why?
This post
Java launch error selection does not contain a main type
does not explain my problem. In my case "main" is well defined.
class Test {
public static void main(String[] args) {
test1();
}
static void test1() {
Font f = new Font("SansSerif", Font.PLAIN, 70);
Glyph g = new Glyph(f, 'g');
System.out.println(g);
}
}
The problem is that the file isn't in a source folder. So for Eclipse, it's just a text file which (by coincidence) contains some Java code. But since the compiler never saw it, there is no .class file -> Eclipse can't run it.
Create a source folder (New... -> Source Folder) or move the file into an existing source folder in your project (they contain a little "package" symbol in the icon) and try again.
The name of the file and the name of the class should match. That is the reason, it's not able to recognize it. Please check the name again.
Haven't used eclipse in some time now as I use InteliJ but make sure you edit configurations and select your main file so Run can then execute your app.

Categories

Resources