I'm trying to learn how to make & use packages in Java. I've experimented with the following "Hello World" program
class helloWorld
{
public static void main (String[] args)
{
System.out.println("Hello World");
}
}
When I compile and run this program in it's home directory everything is fine. However, when I create a sub-directory - ./testPackage and place the following file (hiEarth.java) in it:
package testPackage;
class hiEarth
{
public static void main (String[] args)
{
System.out.println("Hi Earth");
}
}
I can seemingly compile it, but can't run it.
me#ubuntu:~/Projects/JavaProjects/helloWorld/testPackage$ javac hiEarth.java
me#ubuntu:~/Projects/JavaProjects/helloWorld/testPackage$ java hiEarth
Exception in thread "main" java.lang.NoClassDefFoundError: hiEarth (wrong name: testPackage/hiEarth)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: hiEarth. Program will exit.
when I do an 'ls', I see what I expect to see:
me#ubuntu:~/Projects/JavaProjects/helloWorld/testPackage$ ls
hiEarth.class hiEarth.java
Why can't I get java to see a class in the present directory?
When I move one directory above:
me#ubuntu:~/Projects/JavaProjects/helloWorld/testPackage$ cd ..
me#ubuntu:~/Projects/JavaProjects/helloWorld$ java testPackage/hiEarth
Things work fine. I thought this might be a classpath issue, but
me#ubuntu:~/Projects/JavaProjects/helloWorld/testPackage$ java -cp . hiEarth
doesn't work either. What don't I understand here?
Thanks....
Go to ~/Projects/JavaProjects/helloWorld/ and type
java -cp . testPackage.hiEarth
When you execute the java command, you need to provide the fully qualified name of the Java class you want to execute (ie, here, testPackage.hiEarth). The lookup of the classes will be relative to the directories and jars you provide in your classpath argument. Therefore, looking for testPackage.hiEarth will result in this case into ./testPackage/hiEarth.class which will work if relative to ~/Projects/JavaProjects/helloWorld/
NB: Use the Java naming convention and use a capital letter for your class.
Related
I am newbie in java. I am running java programs via Ubuntu terminal
I just started java package topic and have been dealing with a problem for several hours.
I tried to create a simple package called pack which has single class Hello.
I created a directory pack. Inside the pack I put Hello.class file intp pack directory via
javac -d ./pack Hello.java
command.
Then I included pack package into a class containing main method. The class's name is test and it is located in test.java file This class is located in another directory. I compile via
javac -cp ./pack test.java
It compiles without any error and everything is ok.
However, when I enter command
java -cp ./pack test
it gives me
Error: Could not find or load main class test
When I tried
java test
command. The following message showed up
Exception in thread "main" java.lang.NoClassDefFoundError: pack/Hello
at test.main(test.java:6)
Caused by: java.lang.ClassNotFoundException: pack.Hello
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)
... 1 more
Can anybody explain me what I am doing wrong? Any help is much appreciated.
Sorry, I did bot include my source codes. Here they are.
import pack.Hello;
public class test
{
public static void main(String args[])
{
Hello.HelloMessage();
}
}
This is test.java file which tests if everything is ok. It is located at
/home/uesername/apps
directory.
Then I created "pack" directory. Full path to the pack directory is
home/username/apps/pack
Inside the "pack" I put Hello.java file. The content of Hello.java file is
package pack;
public class Hello
{
public static void HelloMessage()
{
System.out.println("hello, world");
}
}
To begin with I suggest you use an IDE to setup you environment for compiling, running and debugging.
The problem you have is that you have compiled with the wrong path.
javac -cp . pack/Hello.java
javac -cp . pack/test.java
and
java -cp . pack.Hello
or
java -cp . pack.test
The problem is that you compiled a class with package pack in a directory pack and you would end up with
pack/pack/Hello.class
I suggest you check where the Hello.class file has been placed.
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?
I'm following the java tutorials on the Princeton website.
I'm running debian sqeeze 64bit and I have installed Sun java version 6.
I can compile and run the basic hello world program without any problem, using the terminal and the Eclipse IDE.
The problem is:
when I try to compile and run a program, which requires an argument input for example:
public class RandomSeq {
public static void main(String[] args) {
// command-line argument
int N = Integer.parseInt(args[0]);
// generate and print N numbers between 0 and 1
for (int i = 0; i < N; i++) {
System.out.println(Math.random());
}
}
}
I can run this on Eclipse putting an integer argument, however it doesn't work on the terminal.
I get this error:
emes#debian:~/Documents/workspace/IOput/src/randomSeq$ java RandomSeq 21
Exception in thread "main" java.lang.NoClassDefFoundError: RandomSeq (wrong name: randomSeq/RandomSeq)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
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)
Could not find the main class: RandomSeq. Program will exit.
I've tried to update the /etc/profile to include the java-6-sun on the PATH variable.
I'm not sure, what to try from here.
Apparently, you're trying to run your program from the src folder of an Eclipse project. src stands for “source”. The executable version of your program (the compiled classes) is not in src; it's in bin, which stands for “binary”, i.e. machine code.
When using the command line, you should first compile your program:
javac MyClass.java
and then run it:
java MyClass
But please, do not do it inside an Eclipse project's directory structure, or you will create additional files (class files) not expected by Eclipse at this location.
Additionally maybe you are inside a package. You can't run a class if you're within its package folder. You need to be at the top-level of the package hierarchy.
Example: suppose your class is inside a package named mypackage. Then in someFolder/mypackage/MyClass.java you will have something like:
package mypackage;
class MyClass {
...
}
After compiling your code, you must be in somefolder and issue the shell command:
java mypackage.MyClass
It looks as if your class has a package
package randomSeq;
public class RandomSeq {
If so, then when starting it it should be located in the folder randomSeq and the root of that folder should be in your class path and the package must be specified when invoking.
So, if your .class file is in bin/randomSeq then you could run it with java -cp bin randomSeq.RandomSeq 21
Don't bother about the argument as that would give a run-time null pointer exception.
The problem is your classpath.
Make a list (ls or dir) in the directory you run java RandomSeq from.
Do you have a .class file there. If not run javac RandomSeq.java first to generate the class file
I am trying to run a test.class file from the linux command line. I compiled the file using javac test.java which produced the test.class file. When I run the command java test it throws a class not found exception. I also tried specifying the package name with the same result. Here is the output, can anybody help? I believe my syntax is correct according to Google searches.
[root#localhost usr]# java test
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: testJava/test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: test. Program will exit.
You need to compile your .java file with javac:
javac MyFile.java
Then run the file using java
java MyFile
If you're doing this and it still doesn't work, that means you need to make sure the file and the class name inside the file have the same name... so MyFile.java contains
class MyFile
{
// ...
public static void main(String[] args)
{
// ...
}
// ...
}
Use
java testJava.test from the parent directory of testJava
Assuming your class looks like this:
package javaTest;
public class test{
public static void main(String[] a){
System.out.println("Test");
}
}
Then to compile and run the file, you basically do this:
$ ls
testJava
$ ls testJava
test.java
$ javac testJava/test.java
$ java testJava.test
Test
What this means is that you have to run the class with its fully qualified name, which includes the package name of the class.You also have to do it from the directory that conatins the root of your package. (Unless you specify the "root" directory with the -cp flag.)
I too had problem initially.
java -cp . Test
import java.io.*;
public class ArrayApp{
public static void main(String[] args){
System.out.println("lllll");
} // end main()
} // end class ArrayApp
i get this error when i run my application after compiling it.
Exception in thread "main" java.lang.NoClassDefFoundError: ArrayApp
Caused by: java.lang.ClassNotFoundException: ArrayApp
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: ArrayApp. Program will exit.
You need to make sure your class file is in the classpath. Assuming you are using the default package (i.e. no package declaration), you need to tell Java where to find your class when you run it. So let's assume your ArrayApp.class file is in the same directory. You will need to run it like this:
java -cp . ArrayApp
The option -cp and the following . tell Java that the classes will be in the current directory. The longer name for -cp is -classpath, so you can use that as well.
Also note the space between the classpath and the class name. The path is the base directory of where your class files are located. If you compiled them into a directory named "bin" then you would change the way you call Java like this:
java -cp bin/ ArrayApp
The "ArrayApp" is the fully qualified class name.
Your classpath is incorrect. Try...java -classpath . ArrayApp