java.lang.ArrayIndexOutOfBoundsException: for jdbc connection - java

I have no idea why I am getting this error, In the program i am not accessing any array. But its still giving this error.
java.lang.ArrayIndexOutOfBoundsException: 0
at oracle.jdbc.driver.OracleSql.main(OracleSql.java:1666)
I have posted the code below.
public class ItT4 {
public static void main(String[] args) {
try {
// loading the class
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (Exception e) {
e.printStackTrace();
}
}//closing main
}//closing class
I have set the class path required for drivers. and even gone through few links in stackoverflow.
Modified
I removed everything and tried printing out the s simple statement. Still i am getting the same error. The code is given below.
public class ItT4 {
public static void main(String[] args) {
System.out.println("hello boss");
}
}

you can follow the following process
click project
click on clean
select your project

Related

How to resolve ClassNotFoundException when the class is there?

Here is my code
package vista;
public class MyMainClass
{
public static void main(String[] args)
{
try
{
if(1>0) throw new MyException("ERROR");
}
catch(MyException err)
{
System.out.println(err.toString());
}
}
}
package vista;
public class MyException extends Exception
{
// Constructor.
public MyException(String errMsg)
{
super(errMsg);
}
}
Output:
Error: Unable to initialize main class vista.MyMainClass
Caused by: java.lang.NoClassDefFoundError: vista/MyException
Command execution failed.
Both classes are in the same \vista folder, and before excecution NetBeans recognizes this. How do I resolve this?
Edit: while not running but just compiling the program I realized NetBeans was attempting to download some files. I turned my firewall off, ran the program, it downloaded some files, and now it excecutes properly.

VS Code still runs Java file even when build fails?

Simple question here. Here is my Java file:
public class Test {
public static void main(String []args) {
System.out.println("It ran!");
}
void a() {
qweifjew;
}
}
When I press "Run" on VS Code, it says build failed do you want to continue? Makes sense since I have compile-time errors. But when I press continue, it is still able to run and display "It ran!". How come?
For more information on the run command:
C:\Users\jeffe\coding-tutorials\learning-jest> cd c:\Users\jeffe\coding-tutorials\learning-jest && c:\Users\jeffe\.vscode\extensions\vscjava.vscode-java-debug-0.27.1\scripts\launcher.bat "C:\Program Files\Java\jdk-11.0.2\bin\java.exe" -Dfile.encoding=UTF-8 -cp C:\Users\jeffe\AppData\Roaming\Code\User\workspaceStorage\5e0a770d0910238b624ead6f98bca1ec\redhat.java\jdt_ws\learning-jest_f8aabfb2\bin Test
It ran!
This is the decompiled .class file of your code:
public class Test {
public Test() {
}
public static void main(String[] args) {
System.out.println("It ran!ddfseffe");
}
void a() {
throw new Error("Unresolved compilation problems: \n\tSyntax error, insert \"VariableDeclarators\" to complete LocalVariableDeclaration\n\tqweifjew cannot be resolved\n");
}
}
You have Auto Save ON in VS code ?
It's able to run a previous successful build to give you an output.

ClassNotFoundException in isInstance

class j {
public static void main(String args[])
{
Object obj=new Object();
String c ="Object";
System.out.println(Class.forName(c).isInstance(obj));
}
}
In the above code i'am trying to find whether obj is an instance of Object or not.I should get the answer as true but i'am getting an error.I am not able to figure out why the error is occurring.Can anyone please help me out?
error: unreported exception ClassNotFoundException; must be caught or
declared to be thrown
System.out.println(Class.forName(c).isInstance(obj));
2 things:
the method forName throws an exception so you need to either catch them or rethrow it.
just doing Class.forName("Object") is not correct, you need to use the fully qualified name of the desired class (i.e with package included)
from the javaDoc
Parameters: className - the fully qualified name of the desired class.
String c = "java.lang.Object";
System.out.println(Class.forName(c).isInstance(obj));
You have to surround the code within try catch or you have to make sure that main can throw excetpion:
try {
System.out.println(Class.forName(c).isInstance(obj));
} catch(ClassNotFoundException ex) {
// do something in case class can not be found
}
or
public static void main(String args[]) throws ClassNotFoundException
You can not leave code, that is able to throw exceptions, without any safety net.
Make sure to use fully qualified name of class:
java.lang.ClassNotFoundException
or import it at the beginning of the source code:
import java.lang.ClassNotFoundException;
The method Class.forName declares that it throws the Exception ClassNotFoundException so in your code you either need to catch it or declare throws ClassNotFoundException in the method declaration. Thus
Public static void main(String[] args) throws ClassNotFoundException {
or
try {
System.out.println(Class.forName(c).isInstance(obj));
} catch (ClassNotFoundException ex) {
System.out.println(ex.getMessage());
}

Java Class.forName method java.lang.ClassNotFoundException;

I've found other thread where people had and solved this error; however, all of were NOT using fully qualified class paths. I cannot seem to get Class.forName to work and I am using fully qualified paths.
I've tested compiling from CLI and using ItelliJ Idea. Both fail with the same error.
Code (in test directory):
package test;
public class Test {
public static void main(String[] args) {
Class cls = Class.forName("java.util.ArrayList");
}
}
Error:
java.lang.ClassNotFoundException; must be caught or declared to be thrown
The example above does NOT work. Thanks in advance!
You're getting this message because ClassNotFoundException is a checked exception. This means that this exception can not be ignored. You need to either surround it with a try/catch construct and provide exception handling or add a throws clause to your method and handle it in a callee.
EDIT:
Please note that Class.forName() construct is not resolved during compilation. When you write such a statement, you're telling the JVM to look during program execution for a class which may not have been loaded. Java libraries are dynamically linked instead of statically linked, meaning that their code is not incorporated in your program code, being loaded only when requested. This is why it throws ClassNotFoundException.
Class.forName("java.util.ArrayList") method declared to throw a checked exception ClassNotFoundException, so you must handle it inside a try/catch block like following
package test;
public class Test {
public static void main(String[] args) {
try {
Class cls = Class.forName("java.util.ArrayList");
} catch (ClassNotFoundException e) {
// Handle it here
}
}
}
Try this:
try{
Class cls = Class.forName("java.util.ArrayList");
}catch(ClassNotFoundException e){
... do messaging or logging
}
or throw ClassNotFoundException in the methods signature:
public static void main(String[] args) throws ClassNotFoundException {
I don't know why none of the answers above can explain why this error happened. Neither can I, but I once encountered this issue and solved it by using
Thread.currentThread().getContextClassLoader().loadClass($YOUR_CLASS_NAME)
Hope it can solve your problem and hope someone can give us an explanation.
There are two options:
Surround with a try-catch block and do some appropriate handling:
package test;
public class Test {
public static void main(String[] args) {
try {
Class cls = Class.forName("java.util.ArrayList");
} catch(ClassNotFoundException e) {
System.out.println("This is not a very serious exception, or at least Test knows how to handle it");
}
}
}
Throw it outside the method in the hope that some calling method will know what to do.
package test;
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
Class cls = Class.forName("java.util.ArrayList");
}
}

JNI Could not find or load main class- Unique issue

I tried lot of options to fix this , but could not find a solution. I have created the header file and the dll too. Set the class path asd well. Javac command works fine. When I run this file, I get error: Could not find or load main class com.log.jni.example.HelloWorld. Could you please help me. Here is my file.
public class HelloWorld {
private native void print(String path);
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String path="C:\\Capture.pcap";
new HelloWorld().print(path);
}
static {
System.loadLibrary("HelloWorld");
}
}
Could it be that your static initializer is failing.
The following code:
public class Main
{
static
{
if (true)
throw new Error("Error is here");
}
public static void main(String... args)
{
System.out.println("I am running");
}
}
produces the output:
Exception in thread "main" java.lang.Error: Error is here
at Main.<clinit>(Main.java:22)
Could not find the main class: Main. Program will exit.
Are there any stack traces printed out before the 'Could not find main class' error? In this example, the class was found but failed to initialize because of the exception thrown in the static initializer. In your code, a likely suspect is that the System.loadLibrary() call fails with an UnsatisfiedLinkError.
The error "Could not find or load main class ..." occurs when the binary file is not built. Click on project, turn off automatic build. Then click on project and build all. Then turn on automatic build.

Categories