Java - Hello World Error - java

I'm currently learning Java. My background is primarily in C#. I'm trying to do a basic "hello world". Currently, i've written the following code in IntelliJ:
import java.io.Console;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world.");
}
}
My app compiles successfully. However, when I attempt to run it, I get a runtime error that says:
Exception in thread "main" java.lang.ClassNotFoundException: com.company.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Process finished with exit code 1
I don't understand what's happening. Can someone please help me?

It seems like you told IntelliJ to call the main method of class com.company.Main. But IntelliJ cannot find such a class. IntelliJ searches for a file Main.class in a folder com/company
Probably you want to tell IntelliJ to run the main method of "HelloWorld" instead of "com.company.Main"...

Go into Edit Configurations (see screenshot) and update your Main class path to be that of the changed package name. In my case, package is "thread" and in there I have a Main class:

You need to specify the package inside your class like this
package com.company;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world.");
}
}

Check these steps in order :
1) Is your file name and your class name is same ?
2) If you work on command line write javac yourfilename.java
3) Then java yourfilename

First Set JDK PATH ..Then compile and run code
JDK Path Setting:
Follow the given steps below to set the PATH
After installing JDK, right click “My Computer” icon
Choose “Properties” from context menu
Click “Advanced” tab and the click ‘Environment Variable’
In the Edit windows, modify PATH by adding the location of the class to the value for PATH. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the class as the value. For example, if you have installed JDK in C drive then path may be in the form C:\Program Files\Java\jdk1.6.0_12\bin
You will have to give your own path in which you have installed JDK.
Open Command prompt window, and run your Java code.

Change your main class from com.company.Main to the class you declared

Related

How to use Eclipse and Java for Mac 10.8.1

When I try to do a simple "Hello World" java application I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: /path/to/mainarg*
Caused by: java.lang.ClassNotFoundException: first.Skeleton
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)
I am running on Mac OS 10.8.1 and am running Java 1.6. I have checked my run configurations to make sure the main argument can be found. For the sake of simplicity I have also tried to use the default JRE of java 1.6 that is included with mac. The build configuration I have used is also correct and pointed to the right place.
When I followed this tutorial online, and executed the commands over the terminal I get the same error. What am I missing? I don't think it is an Eclipse problem anymore, but something bigger. Any advice would be greatly appreciated.
Here is the code I tried to run:
public class Test{
/* This is my first java program.
* This will print 'Hello World' as the output
*/
public static void main(String[]args){
System.out.println("Hello World");// prints Hello World
}
}
I put the code on my Desktop for simplicity. I went to /usr/tyler/desktop then made the script an executable with "javac Test.java", and when I tried to run it with "java Test.java" I then got the error.
Here are the other tutorials I have followed:
-Java Compilation error on Mac
-HelloWorld with Eclipse on Mac
-Along with many others from SO and elsewhere
EDIT:
Thank you for the help. The terminal issue has been resolved. However, if anyone has any advice on what could be going wrong with Eclipse, that would be appreciated.
You need to run ...
"java Test"
You cannot execute the java code, you need to execute the compiled class file. The java executable will look for a .class file with the name Test.
execute or run
java Test
instead of
java Test.java

How do I run a Java class in a package?

I have two java classes as follows
App1 without a package:
class App1 {
public static void main(String[] args) {
System.out.println("App1 hello world...");
}
}
App2 in a package:
package java.java.package1;
class App2 {
public static void main(String[] args) {
System.out.println("App2 hello world...");
}
}
I can compile them both:
D:\javaTest>javac App1.java
D:\javaTest>javac App2.java
However, I can only run the first one:
D:\javaTest>java App1
App1 hello world...
D:\javaTest>java java.java.package1.App2
Exception in thread "main" java.lang.NoClassDefFoundError: java/java/package1/App2
Caused by: java.lang.ClassNotFoundException: java.java.package1.App2
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: java.java.package1.App2. Program will exit.
How can I run App2?
If you put the source in an appropriate directory hierarchy matching the package name (D:\javaTest\java\java\package1\App1.java), and compile/run from the root of the hierarchy (D:\javaTest), you wouldn't have this problem:
D:\javaTest>javac java\java\package1\App1.java
D:\javaTest>java java.java.package1.App1
App2 hello world...
You can also compile using the -d option so that the classes are moved into such a directory hierarchy:
javac -d . App2.java
java java.java.package1.App2
Note you shouldn't use a package name starting with java, and later versions of the JDK will throw a SecurityException. See this question for more information.
You create a new directory. This is the directory containing your work, and is not the start of your packages.
For instance, I create folder /terri to start.
I then create the folder structure /clarie/andrea under it.
My package is going to be called claire.andrea in this example.
Normal package names start with com and then a company name or something like that (or java for standard java packages, so don't use that: like java.lang.*).
In the andrea folder, I create a java file called Saluton.java with the class Saluton (which just print hello). The class name and the filename must match.
To compile, from the terri/ folder: javac .\claire\andrea\Saluton.java
This will create a Saluton.class in the \terri\claire\andrea\Saluton.class
To run: (again from /terri), I do: java -cp . claire.andrea.Saluton
Which says, use class path from my current directory.
My main program is in the package claire.andrea and the Class name is Saluton.
Here's the run:
\terri
java -cp . claire.andrea.Saluton
"Hello World".
To sum it up, the package name much match the underlying directory structure.
The file (if it references a package) must live inside the directory stucture it is refering. If I compile Saluton.java in /terri with package claire.andrea I have not found a way to run it, it does compile fine.
Also, the filename for the class must match the public class in that file.
To run, package.Class. In general, packages are not capitalized and Classes are.
You have to put -d directory Options..because
Set the destination directory for class files. If a class is part of a
package,
javac puts the class file in a subdirectory reflecting the package name,
creating directories as needed. For example, if you specify -d c:\myclasses
and the class is called com.mypackage.MyClass, then the class file is called
c:\myclasses\com\mypackage\MyClass.class.
App2 needs to be in the java/java/package1 directory. In java, the package name and the directory for the source code must match. Did you not get a compilation error?
Check what is your classpath value by below command in command prompt
echo %CLASSPATH%
check where your class is getting created.
to compile a java source program you need to check the path you are giving , whether java file is available there or not.

Java - running my prog from terminal and getting the following error messages (w/ NoClassDefFoundError) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
NoClassDefFoundError , Java
> java foo/boo/Prog
Exception in thread "main" java.lang.NoClassDefFoundError: foo/boo/Prog
Caused by: java.lang.ClassNotFoundException: foo.boo.Prog
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: foo/boo/Prog. Program will exit.
I'm really lost. I have a directory foo/boo/ and in that I have Prog.class along with some other dot-class files. foo is a subdirectory of the current directory where I run the command from. I compiled it fine, and in my foo/boo/ directory I've checked to make sure that there is indeed a "Prog.class" along with all the other dot-class files I need. There is a main method in my Prog class, and I'm pretty sure this problem has nothing to do with my source code (although it could of course) because I was able to run Prog fine in eclipse, just not from my terminal (ssh-ing onto another machine).
Could someone try to decipher what all of that jumble might mean? I don't really understand. Thank you very much.
$ java -h
Usage: java [-options] class [args...]
(to execute a class)
You are trying to specify the filesystem path to your class file, this is not possible. You need to specify the classpath correctly, so the class can be found by the classloader.
The classpath is a set of paths, where the java classloader looks for the classes to load. So specify the correct folders after the -cp parameter and it will be fine.
I'm probably misunderstanding (and don't have enough to just comment on questions) but are you saying you have a Prog directory, with a Prog.class in there? Wouldn't that make it foo.boo.Prog.Prog ?
Start from outside of foo/boo/Prog, i.e. having current directory being the parent of foo and run as #grtt1 said.
SAMPLE THAT WORKS
suzan#nebulla:~/Test_Java_01$ ls
foo
suzan#nebulla:~/Test_Java_01$ ls foo
boo
suzan#nebulla:~/Test_Java_01$ ls foo/boo
Prog.class Prog.java
suzan#nebulla:~/Test_Java_01$ cat foo/boo/Prog.java
package foo.boo;
public class Prog {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
suzan#nebulla:~/Test_Java_01$ java foo.boo.Prog
Hello world

Understanding JARs and Packages in Java

I'm trying to understand how jars and packages work in Java. So to do this, I created a simple test JAR and am trying to use a class contained in that jar. Simple enough, but it is giving me errors like "class not found". Here's the setup:
1) I have a file called MyHelloWorld.java, which will be packaged in a JAR:
package com.mytest;
public class MyHelloWorld {
public String getHello() {
return "Hello";
}
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
2) I have another file called 'HelloHello.java' which uses the function getHello() in com.mytest.MyHelloWorld
import com.mytest.*;
public class HelloHello {
public static void main (String[] args) {
MyHelloWorld hello = new MyHelloWorld();
System.out.println(hello.getHello());
}
}
3) To package the MyHelloWorld class inside a JAR, I created the folders com/mytest in the current directory, and moved MyHelloWorld.java to that folder
4) I compiled MyHelloWorld.java in that folder using javac MyHelloWorld.java
5) I ran jar -cf myhello.jar ./com/mytest/*.class from the root folder to create the JAR file (as described in http://www.javacoffeebreak.com/faq/faq0028.html)
6) I copied HelloHello.java and myhello.jar to a new folder with nothing else in it, to test this setup
7) javac -cp ./*.jar HelloHello.java [succeeds]
8) java -cp ./*.jar HelloHello [FAILS] (I also tried just `java HelloWorld', which failed too, with a different error message)
This last statement fails with the message:
$java -cp ./*.jar HelloHello
Exception in thread "main" java.lang.NoClassDefFoundError: HelloHello
Caused by: java.lang.ClassNotFoundException: HelloHello
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
Any idea why it's failing? Any insights you can provide on why it works this way, and how package names are defined inside a JAR etc. would also be appreciated!
I believe it is looking in the jar for your HelloHello class. You probably need the current folder on the classpath too.
java -cp .:myhello.jar HelloHello
You should use:
java -cp .:./* HelloHello
java and javac treat -cp argument a bit differently. With java the * in cp will automatically load all the jars it finds in the given location.
Also, the colon : is the separator between different classpath elements.
Make sure if HelloHello.class is in appropriate directories structure (com/mytest) than change your 8th step:
8) java com.mytest.HelloHello //or java -cp .;*.jar com.mytest.HelloHello
well, java HelloHello works too

Using a jar in a Java project?

I'm trying to use the public methods/classed from a project provided as a jar file (called Hello.jar for instance) wrapped in a package called hello.
package hello;
public class Hello
{
public static void main(String[] args)
{
coucou();
}
public static void coucou()
{
System.out.println("Hello there");
}
}
In a separate project called Tool, I want to be able to call the method Hello.coucou() so I wrote something like this:
import hello.*;
public class Tool
{
public static void main(String[] args)
{
System.out.println("main program running");
Hello.coucou();
}
}
and I compiled Tool.java with the following command (under linux):
$ javac Tool.java -classpath .:./extern/:
where Hello.jar is located in the folder ./extern
This seems to compile fine but when I launch it (i.e. java Tool), I get this:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
I am new to Java (C/C++ background) and I don't understand what I'm doing wrong.
Any ideas?
Cheers
David
Edit: I tried adding Hello.jar to the classpath on the command line, but I still get the same error:
$ javac Tool.java -classpath .:./extern/Hello.jar:
$ java Tool -classpath .:./extern/Hello.jar:
main program running
Exception in thread "main" java.lang.NoClassDefFoundError: hello/Hello
at Tool.main(Tool.java:9)
Caused by: java.lang.ClassNotFoundException: hello.Hello
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)
... 1 more
You need the Hello.jar on the classpath when you run as well as when you compile.
Actually the trick was in the order of the arguments in the command line:
Is the -cp (or -classpath) is set last, then it doesn't work
java Tool -cp .:extern/Hello.jar
It has to be first like:
java -cp .:extern/Hello.jar Tool
!!!
Java uses dynamic late binding, so putting the JAR in the classpath during compilation is only necessary to ensure that your code is using the classes from it correctly, but it does not actually embed them into your code as the linker would in C/C++. Thus, you need to set the classpath also when executing the code.
However, this:
$ javac Tool.java -classpath .:./extern/:
should not work either, since JARs need to be put into the classpath directly, not just the directory they live in:
$ javac Tool.java -classpath .:./extern/Hello.jar
Finally, you are placing your code in the default nameless package. This is OK for fooling around, but will cause problems in the long run (for one thing, you cannot import classes FROM the default package anywhere else).
When you run Java you must add the jar file too (adding the directory path only does not work).
See classpath information.
It should be something like this:
java -classpath /java/MyClasses/myclasses.jar utility.myapp.Cool
You need to include the Hello.jar file in the classpath when you launch it too.
java -cp xxx.jar hello where xxx is the jar you want to have in your classpath, if you want multiple jars then separate them using ;
karl

Categories