Exception in thread "main" error - java

This is the only error that I see in my code. It is this line:
public class VolCac extends JFrame implements ActionListener{
This is the error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at VolCac.main(VolCalc.java:810)
This is line 810:
public static void main(String[] args){
JFrame frame = new CalcVolume();
frame.setSize(525, 350);
frame.setVisible(true);
}
}
I can’t figure why I’m getting the error.

"Unresolved compilation problem" errors at rntime are what happens when your code doesn't compile, but you tell Eclipse to run it anyway. The exception will happen at the point where the program can't continue any longer.
In this case, I see a file named "VolCalc," a class named "VolCac", and an attempt to create an instance of "CalcVolume". I'm pretty sure you mean for all three of these to match!

That error implies you're running Eclipse. Regardless, your IDE should be telling you what's wrong on that line. I hope you don't really have ALL that code on one line as you imply!

if you're using eclipse try to clean your project
and also your class name is VolCac and your are makine a new CalcVolume Object...
can't say much without seeing more code...

Related

glfwInit() is throwing an IllegalStateException error on initialization

I am trying to set up GLFW, but so far, I am getting stuck on the very first step, which is calling glfwInit(). My code is below:
import static org.lwjgl.glfw.GLFW.*;
public class Main {
public static void main(String[] args)
{
if(!glfwInit())
{
throw new IllegalStateException("Failed to initialize GLFW");
}
}
}
I am getting this error: Exception in thread "main" java.lang.ExceptionInInitializerError at org.lwjgl.glfw.GLFW.glfwInit(GLFW.java:1046) at Main.main(Main.java:6) Caused by: java.lang.IllegalStateException: GLFW may only be used on the main thread and that thread must be the first thread in the process. Please run the JVM with -XstartOnFirstThread. This check may be disabled with Configuration.GLFW_CHECK_THREAD0. at org.lwjgl.glfw.EventLoop.<clinit>(EventLoop.java:30).
For reference, I am on a Mac, and am using eclipse, and have every single LWJGL jar file included in the project classpath.
Will running JVM with -XstartOnFirstThread like the warning says fix the issue? And if so, how do I do it?

without main class in java throws error while running [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Printing message on Console without using main() method
Can someone suggest how can a JAVA program run without writing a main method..
For eg:
System.out.println("Main not required to print this");
How can the above line be printed on console without using the public static void main(String arg[]) in the class.
Up to and including Java 6 it was possible to do this using the Static Initialization Block as was pointed out in the question Printing message on Console without using main() method. For instance using the following code:
public class Foo {
static {
System.out.println("Message");
System.exit(0);
}
}
The System.exit(0) lets the program exit before the JVM is looking for the main method, otherwise the following error will be thrown:
Exception in thread "main" java.lang.NoSuchMethodError: main
In Java 7, however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:
The program compiled successfully, but main class was not found.
Main class should contain method: public static void main (String[] args).
Here an alternative is to write your own launcher, this way you can define entry points as you want.
In the article JVM Launcher you will find the necessary information to get started:
This article explains how can we create a Java Virtual Machine
Launcher (like java.exe or javaw.exe). It explores how the Java
Virtual Machine launches a Java application. It gives you more ideas
on the JDK or JRE you are using. This launcher is very useful in
Cygwin (Linux emulator) with Java Native Interface. This article
assumes a basic understanding of JNI.
Up until JDK6, you could use a static initializer block to print the message. This way, as soon as your class is loaded the message will be printed. The trick then becomes using another program to load your class.
public class Hello {
static {
System.out.println("Hello, World!");
}
}
Of course, you can run the program as java Hello and you will see the message; however, the command will also fail with a message stating:
Exception in thread "main" java.lang.NoSuchMethodError: main
[Edit] as noted by others, you can avoid the NoSuchmethodError by simply calling System.exit(0) immediately after printing the message.
As of JDK6 onward, you no longer see the message from the static initializer block; details here.
public class X { static {
System.out.println("Main not required to print this");
System.exit(0);
}}
Run from the cmdline with java X.
Applets from what I remember do not need a main method, though I am not sure they are technically a program.

Java run time error.missing static main method

https://github.com/joywang1994/question2/blob/master/test2.java
This is my code. It has a run time error which says I don't have a static main method
Why? How could I fix it?
Thank you for your help!
I guess you are running wrong class file to start the program. I create 3 class file with the name HuffmanTree,HuffmanNode and Huffmancode. Copying it from your GITHUB. I ran Huffmancode class file. Program compile and runs with out error. I dont have C:\eclipse\Programmes\Datastructure\infix.dat this file so i got file not found exception. Ignoring that my main method called without any trouble.
If the message is:
Static Error: This class does not have a static void main method accepting String[].
then it follows that you are (somehow) running the wrong class. But unless you show us the complete stacktrace, and explain how you are running the code, we cannot tell you what you are doing wrong.

error messages are displaying different in both eclipse and command prompt

Sub: error messages are displaying different in both eclipse and command prompt
//DataHidingDemo program
class Bank {
private static double balance = 1000;// Data Hiding
}
public class DataHidingDemo extends Bank {
public static void main(String[] args) {
System.out.println("Balance:" + balance);
}}
Case 1:
run the program from eclipse and observe below error message is displaying
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Bank.balance is not visible
at oops.DataHidingDemo.main(DataHidingDemo.java:10)
Case 2:
run the same program from command prompt and observe below error message is displaying
D:\Java Programs_CMD>javac DataHidingDemo.java
DataHidingDemo.java:10: error: balance has private access in Bank
System.out.println("Balance:"+balance);
^
1 error
Observe both case 1 & 2 error messages; case 2 error message has meaningful.
Q).Do we have the way to display the same error message in eclipse too? (for this do we need to change any setting in eclipse) please help on this.
Eclipse has its own Java compiler, which is thus different from javac, and thus generates different error messages. AFAIK, no, it's not possible to make Eclipse use the javac compiler. Both NetBeans and IntelliJ IDEA use javac, though, so you might want to try thise IDEs instead.
Note that the error you got from Eclipse is a message generated when you tried running code that did not compile. Don't do that. If there are compilation errors listed, then fix them all before running. The compilation errors are listed in the "Problems" view and in the "Markers" view of Eclipse.

ArrayIndexOutOfBoundsException on every main() function I create

This problem only started recently, but all my main functions on various programs don't work (they have worked in the past).
They now all return:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at com.avaje.ebeaninternal.server.lib.sql.Prefix.main(Prefix.java:40)
Here is an example of a class that gives this error:
package PACKAGE_NAME_HERE;
public class SomeClass {
public static void main(String[] args) {
System.out.println("Test");
}
}
You're calling method main in com.avaje.ebeaninternal.server.lib.sql.Prefix (which is part of Maven, source can be found here) from somewhere.
Line 40: String m = e(args[0]);
And if you don't pass any command-line arguments, you don't have args[0], and you got ArrayIndexOutOfBoundsException.
That's all I can tell from your question(can't use comments yet, but more clarification/what did you change recently(anything about Maven?) would be nice).
Deleting the project and making a new one fixed this error. Not sure why, maybe I accidentally messed up the setup or something.

Categories

Resources