ArrayIndexOutOfBoundsException on every main() function I create - java

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.

Related

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.

Netbeans Error: Could not find or load main class

When I run the below code, I get the error that Could not find or load main class. I have removed the package and created it again. But the error is still exist. I did some methods to fix it such as right clicking on package name -> properties -> run option to change the main method but there is nothing. But if I create another package name and write this code in it, the program work.
package craps;
public class Craps {
public static void main(String[] args) {
int number = 10;
System.out.println(number);
}
}
Your code is not having any errors
I don't know what is happening in Netbeans .I have been using this for years and living with this kind of errors.
perhaps you get this when netbeans running out of memory and that particular moment you are editing this file.
My workaround for this kind of errors are
1.Do some dummy editing in that file like commenting some empty line // and save All and recompile it
2.Close and open this project (Sometimes work)

Error: Could not find or load main class hello.world.HelloWorld

I am trying to run this project called "hello user". I am new to Java, so wrote a simple program that takes your name, and displays "Hello ". while Running it, I get the following error:
run:
Error: Could not find or load main class hello.world.HelloWorld
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
But when I run file HelloWorld.java, it does it fine
I am doing this on Netbeans IDE 7.2
Rather than the coding error, it could be related to IDE. Since the "Run File" runs okay, but 'Run Project" does not, I believe you have something to set up in IDE itself. Right click the project, and select "Set is as Main", now run the project. I am just giving it a guess, may not help you. But it worth a shot.If it does not help, please paste your code too.
Your class needs a public static void main(String[] args) function. And moreover I suspect that the error could be in the package.
If you want your class in <main_package>.<sub_package>, The directory structure is
- main_package
- sub_package
-HelloWorld.java
And be sure to write your class like this.
package main_package.sub_package;
public class HelloWorld {
public static void main(String[] args){
System.out.println("Hello " + args[o]);
}
}
This is all due to the naming convention in Java
You need to run the .class file containing the public static void main(String[] args) method..
Here, your HelloWorld.java file might contain a class with main() method.. So, you can run it..
This is because, execution of any Java program starts with the invocation of main().. JVM needs an entry point to your code.. Which is main().. If it doesn't find one.. It will not run..
So, make sure, whatever class file you are running, it should have main() method..
UPDATE :- And for the starting point, may be you can skip using packages.. Just go with plain Java class without packages..
This message can also appear in Eclipse (Juno 4.2.2 in my case) and I have found two potential causes for it.
In my cases:
1. a DTD was in error. I deleted the file and that solved the issue*.
2. having cleaned the project, an external Jar that I had built externally had been deleted as could be seen from Properties -> Java Build Path -> Libraries.*
*Having solved either of the above issues, it was necessary to restart Eclipse
if you are using intellij idea then just rebuilding (clean and build) project might solve your problem . because intellij might be still trying to load the old classes which are not there or changed
Make sure you call looks like below:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello user");
}
}
To run a Java class in stand alone mode, public static void main(String[] args) is the entry method, which is must.

How can you run a Java program without main method? [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.

Exception in thread "main" error

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

Categories

Resources