cannot run a simple java code - java

I have downloaded a java developers kit for my 64bit windows 7, wrote down my code in the notepad, though the code is compiling from the command prompt and creating a .class file, but its refusing to run showing the error code:
java.lang.NoClassDefFoundError: first Caused by: java.lang.ClassNotFoundException: first
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: first. Program will exit. Exception in thread "main"
I have made sure more than once that the file name and the class name are exactly same(i have kept them smallcase 'a' just to be sure). But still no avail, could you please suggest a few solutions please.. I'm new to java i'm basically a C/C++ programmer.

A java program has this basic structure:
ClassName.java
public class ClassName
{
public static void main(String[] args)
{
}
}
try using this outline to generate your code.
compile and run with:
javac ClassName.java
java ClassName

I used to get this error when I ran a Class file.
Try doing: java NameOfClass
You don't need the .java extension when running, but you need it for compiling. That was always the problem I used to have.

Did you set your classpath?
http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
java -classpath <path> <classname>

You must provide with the code.
Anyway, from the Java Docs, class ClassNotFoundException:
Thrown when an application tries to
load in a class through its string
name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader.
The loadClass method in class ClassLoader.
but no definition for the class with the specified name
could be found.
Must read link : Tip: Causes of java.lang.ClassNotFoundException

It is quite possible after compiling your code you would be writing java filename.java.
Because this sought of exception occurs then.
After compiling your program using javac filename.java use the command to start your interpreter java filename.
As you enter this command java interpreter automatically starts interpreting filename.class.
commands :
javac filename.java // to start compiling
java filename // to start interpreting

Related

exception in thread 'main' java.lang.NoClassDefFoundError

When I try to execute the following program from DOS I get the results below..
The following program is in C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src\java1
package java1;
public class MyProgram{
public static void main(String[] args){
System.out.println("Rome wasn’t burned in a day!");
}
}
javac MyProgram.java
works fine
But java MyProgram gives the following:
Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram (wrong name
: java1/MyProgram)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
................
Why this is happening??
My environment variables:
CLASSPATH: .;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files\Java\jdk1.7.0_05\bin
PATH: C:\Program Files\Java\jdk1.7.0_05\bin
JAVA_HOME: C:\Program Files\Java\jdk1.7.0_05
I have seen similar problems here but i cannot find the solution to my problem.
Thank you in advance!
This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.
Refer this Link
2 points you should keep in mind when using java tool:
Add the class to the classpath.
Use the fully qualified name of the class to be run.
Hence:
java -cp C:\Users\Apostolos\Documents\NetBeansProjects\Java1\bin java1.MyProgram
assuming the following file exists after compilation:
C:\Users\Apostolos\Documents\NetBeansProjects\Java1\bin\java1\MyProgram.class
For more info, see:
Mastering the Java CLASSPATH.
"Setting the class path" Documentation.
NoClassDefFoundError in Java comes when Java Virtual Machine is not
able to find a particular class at runtime which was available during
compile time. For example if we have a method call from a class or
accessing any static member of a Class and that class is not available
during run-time then JVM will throw NoClassDefFoundError.
Obvious reason of NoClassDefFoundError is that a particular class is not available in Classpath, so we need to add that into Classpath or we need to check why it’s not available in Classpath if we are expecting it to be. There could be multiple reasons like:
Class is not available in Java Classpath.
You might be running your program using jar command and class was
not defined in manifest file's ClassPath attribute.
Any start-up script is overriding Classpath environment variable.
Try in this way
run command prompt as Administrator, and
cd C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src
then
javac java1/MyProgram.java
Then
java java1.MyProgram
This will work.
Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram (wrong name
This exception is thrown when the JVM cannot finf your class at run time
From C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src
execute " "java java1.MyProgram" –

Java reading an argument from terminal

I'm trying to implement the following:
public class Main {
public static void main(String[] args) {
//READ FILE IN
String filename = args[0];
System.out.println(filename);
}}
This compiles fine, but when I try to run java br/com/seimos/minijava/Main.java < a or java br/com/seimos/minijava/Main.java a for example, I get an error. Why?? (by the way, I need to get it so that I can do java br/xx/xx.../xx.
Thanks!
EDIT: Sorry, I typed it wrong initially. I DID run java not javac.
The error I get is:
Exception in thread "main" java.lang.NoClassDefFoundError: br/com/seimos/minijava/Main/java
Caused by: java.lang.ClassNotFoundException: br.com.seimos.minijava.Main.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
You don't pass arguments at compile-time, but at runtime.
javac --> compiles your Java program
java --> runs the generated bytecode
java br/com/seimos/minijava/Main.java -args
^ remove (.java)
Java runtime runs the .class bytecode-generated, which is of the name supplied, not the uncompiled .java source
Running a Java program is a two step process. First the .java file is compiled into .class files. Then you use the java command to execute the class files. Runtime arguments must obviously be passed at runtime when you invoke java.
For compiling, if the java file is with some package, you need to apply such as br.com.xxx.main.java. If that is just path to the java file that is ok. can you post the error?

Execute Java Class from VB.Net

I have a class in java "Main.class", wrote and stored in %TEMP%. When executing the class through VB.Net Shell, eg:
Shell("cmd.exe /k java %TEMP%\Main.class")
Also when trying to execute manually through CMD: "java %TEMP%\Main.class", I am returned with:
Exception in thread "main" java.lang.NoClassDefFoundError: C:\Users\Ben\AppData\
Local\Temp\Main/class
Caused by: java.lang.ClassNotFoundException: C:\Users\Ben\AppData\Local\Temp\Mai
n.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: C:\Users\Ben\AppData\Local\Temp\Main.class. Prog
ram will exit.
However, when I execute Main.class manually through compile.bat - the class runs fine. What is the reasoning for this?
You need to add -classpath to it. Basically the Java interpreter does not know where to find this "Main" class.
CoolBean's solution should work. Something like
Shell("cmd.exe /k java -classpath %TEMP% Main")
Since you can (and are supposed to) omit the .class extension. And like CoolBeans said, you set the directory your class file lives in as the classpath.
While javac takes a file, java takes a Class (in other words, the name of the class after 'public class'), along with what packages it is in (if you don't have "package something;" at the top of your java file, don't worry about this), and it'll look for that class in the classpath you provide, or the current working directory.
If that does end up being the solution, give CoolBeans the accepted answer.
However, an alternative solution is to change the current working directory for Shell to %TEMP%, like:
IO.Directory.SetCurrentDirectory(Environ("TEMP"))
Shell("cmd.exe /k java Main")
Or alternatively look into the Process class, which offers more fine control over launching other programs (and with Process you can also change the directory of the program you're launching without changing the current directory of your own application).
Try this,
Shell("java.exe -cp .;" & Environment.GetEnvironmentVariable("TEMP") & " Main")
OR
Dim args As String = String.Format("-cp .;{0} {1}", Environment.GetEnvironmentVariable("TEMP"), "Main")
Dim procInfo As New ProcessStartInfo
procInfo.FileName = "java.exe"
procInfo.Arguments = args
Dim proc As New Process
proc.StartInfo = procInfo
proc.Start()
proc.WaitForExit()

How to use SQLite3 with Java

I am trying to build a simple java program which creates a db file, then a table and inserts dummy values in the table. I found this page http://www.zentus.com/sqlitejdbc/index.html and tried out the example given on the page but I am getting the following error -
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: Test. Program will exit.
Well that looks like it's a matter of the classpath not being right.
My guess is that you're
If you've put something like
java -cp sqlitejdbc-v056.jar Test
then you probably just need to add the current directory to the classpath:
# Windows
java -cp sqlitejdbc.jar-v056;. Test
# Unix
java -cp sqlitejdbc.jar-v056:. Test
Having looked at that page, my guess is that you used : as the classpath separator, as shown on the page, rather than ; which you need to use if you're on Windows.
Are you sure you're building the test correctly? Here are the steps you'll need to take:
Create a file named Test.java, because it will contain the class named Test
Paste the Java code from the Getting Started section into this file and save it
Download the sqlitejdbc-v056.jar file
and put it in the same directory as the Test.java file
Use javac to compile Test.java into Test.class
Run the command from the Getting Started section
You should see:
name = Gandhi
job = politics
name = Turing
job = computers
name = Wittgenstein
job = smartypants
as output.

java - missing main class

I'm trying to run one of the very first examples from the book "Head First Java";
public class MyFirstApp {
public static void main (String[] args){
System.out.println("I Rule!");
System.out.println("The Worlds!");
}
}
"javac" created a .class file from the .java file - but "java" complains about a "missing main class" when trying to run the .class file (i also tried java -cp . "..." with the same result):
C:\>java \hfj\MyFirstApp.class
Exception in thread "main" java.lang.NoClassDefFoundError: \hfj\MyFirstApp/class
Caused by: java.lang.ClassNotFoundException: \hfj\MyFirstApp.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: \hfj\MyFirstApp.class. Program will exit.
You need to run it as
javac MyFirstApp.java
java MyFirstApp
From the directory where MyFistApp.java lives.
'javac' calls the compiler - to that you need to pass your .java file.
'java' will run the compiled code - to that you pass the name of your compiled file, but without any extension: "java MyFirstApp"
Specifying the full path of the file should work when you are not in that directory. But are you in the directory that holds the javac and java programs? If not, those may also need absolute paths if you have not put them on your PATH variable.
What ts the package name? Maybe you have something like
package org.test;
in your header?

Categories

Resources