I have two files, a.groovy and b.groovy. The first one is a.groovy and I put it in folder abc.
package abc;class A {void p (){print("haha")}};
The second one is b.groovy. I put it in the root folder.
import abc.*; class B {void p(){new A().p()}}; new B().p();
My question is how to run b.groovy. I tried to call "groovy b.groovy -cp abc", but it stills can not run as it can not resolve class A.
What should I do ?
The groovy command line utility is looking for a file named A.groovy to load your class. Failing that, it'd look for a class named A in the classpath, but since A isn't compiled yet it won't find it.
Change the filename to A.groovy and it should work. You shouldn't need the -cp flag in that case.
Related
There is a class a.b.c.Hello of which I would just get the Hello.class file delivered. I would like to run using something like:
java Hello
//or
java a.b.c.Hello
This leads me to a NoClassDefFoundError. Normally I'd have the a/b/c dirs with the class insdie and I'd add them to the -classpath option, but:
Is there a way of running a class in a package like this without having to put it inside a/b/c to match the package name?
All solutions I've found state the directory structure has to match the package naming, but I'd like to run the .class file directly from the folder where it is without recreating the folder structure
Is there a way of running a class in a package like this without having to put it inside a/b/c to match the package name?
Yes: By using a jar. Put the .class file in the jar (with the correct path), and then:
java -cp TheJarFile.jar a.b.c.Hello
This is, of course, very much like putting it in an a/b/c directory; it's just that the directory is in the jar, not the file system.
The packages do not just structure your classes (in folders), they also create a namespace. While the simple class name is Hello, the real class name is a.b.c.Hello.
That's because class names might repeat frequently (in different libs, f.e), but must be addressable on the other hand. For example: 'User', 'Logger', 'List', 'Date'.
Its not recommended, but you can put your class in the default package, too. Just remove the 'package ...' line.
So I'm completely new to programming, and I've been writing some Java with the NetBeans IDE. My code runs fine within NetBeans, but I've tried to run it using the command line as well. However, if I run it from the command line, I have to delete the line:
package firstprogram;
which NetBeans automatically places at the top of each new file, or I get the error:
Error: Could not find or load main class FirstProgram
However, if I do delete the line, then the program no longer runs within NetBeans! It doesn't seem right that I have to choose whether to run a .java from within NetBeans or without.
The research I've done makes me think that this is something to do with directory structure? But everything I read on that goes straight over my head. NetBeans has a structure with "build", "dist", "nbproject", and "src", but when I use the command line I just place the .java file in an empty directory and javac from there.
Any explanation is appreciated! The books and tutorials I'm learning from either assume you're just using NetBeans or don't have the package line at all.
You can compile your class using javac command from anywhere, as long as you provide correct relative or absolute path. The problems come when you want to run your program using the java program.
You have to provide the correct path corresponding to your package declaration. For example, if I had 'MyClass' in package mypackage, first line would look like this:
package mypackage;
class source stored on disk:
c:/MyNetbeansProject/src/mypackage/MyClass.java
Compiled bytecode:
c:/MyNetbeansProject/build/classes/mypackage/MyClass.class
Now, if I would have opened a command prompt/terminal in folder c:/MyNetbeansProject/build/classes/, I could run the program using java mypackage/MyClass or java mypackage.MyClass.
However, if i would be somewhere else, I would have to say where the class files are located using the cp option: java -cp c:/MyNetbeansProject/build/classes mypackage/MyClass. The path in cp option can be relative or absolute, use "" when it contains spaces.
Package are directory architecture.
If your class is in the package com.acme.test, the class should be in the com/acme/test directory.
Instead of placing your class in an empty folder, place it in a folder named firstprogram and do javac firstprogram/youclass.java
The package (and folder) permit you to arrange your architecture with logical pattern.
More info here : http://www.tutorialspoint.com/java/java_packages.htm
So like OcterA said, you should keep organized, but with only one class this is not the issue. I believe that your problem is that you are not entering the correct command into the command line.
First cd to the correct directory and when you want to execute a file within a package in that directory you need to enter
java packageName.className
In this case
java firstprogram.FirstProgram
I am trying to compile and run some java files I have made in Eclipse. The full path to the .java file is C:\Users\MYNAME\Documents\Java\Introduction\src\tests\Test.java. tests is the package I created in Eclipse and src is a folder that Eclipse made under Introduction (which is the project name).
In my environment variables, I have the following relevant variable:
JAVA_HOME C:\Program Files (x86)\Java\jdk1.7.0_40\bin
Under system variables I have the following:
CLASSPATH %JAVA_HOME%
I go to my cmd and cd into the tests directory (cd C:\Users\MYNAME\Documents\Java\Introduction\src\tests). Then I compile using javac Test.java. This seems to work as I then have a Test.class file under the same directory. Now I want to run the file, I type java Test and I get the error, "could not find or load main class". I've tried a variety of things including appending .class and .java to the end but I keep getting the error. I looked at some answers and docs and I managed to get it to work if I cd into:
cd C:\Users\MYNAME\Documents\Java\Introduction\src (i.e, get out of the package)
and then run:
java -cp . tests.Test
So that seems to temporarily set the class path to the current directory, and run Test from the package tests. However, I want to simply be able to type java Test. I know it's possible as I used to be able to do it, but now for some reason I cannot (I must have changed something along the way...).
Any help is appreciated.
However, I want to simply be able to type java Test
That will only work if Test is in the default package - it's as simple as that. You need to pass the java executable the fully-qualified name of the class you want to launch. There's no way round that.
Of course, you could create your own launcher which looks in the current directory for class files, finds out the fully-qualified name of the classes within those files, and launches java providing the full name and probably specifying an appropriate classpath... but that seems like a lot of hassle compared with just including the package name in the command.
You could be making the same mistake I made. So, try the following.
Here is my code for your reference.
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
Once you saved this as "C:\JavaStudy\ClassA.java", try the following.
c:\JavaStudy>javac ClassA.java
c:\JavaStudy>java A.class
Error: Could not find or load main class A.class
c:\JavaStudy>java A
Hello world
c:\JavaStudy>
Note: You don't need to use " java.exe -cp . " if you have class file in the same directory from where you are executing.
I have read the previously posted questions. Some are vague and none solved my problem so I am forced to ask again.
I have two simple classes,
package One;
import One.Inner.MyFrame;
public class test
{
public static void main(String args[])
{
MyFrame f= new MyFrame();
}
}
And the other class is,
package One.Inner;
import java.awt.*;
import javax.swing.*;
public class MyFrame extends JFrame
{
public MyFrame()
{
setPreferredSize(new Dimension(400,560));
setVisible(true);
}
}
I am at base folder "basic" in Windows cmd. I compile using
basic> javac *.java -d .
A folder and subfolder is created.
cd One
basic\One> java test
This generates a big set of errors. Many answers directed to specify the full path which didn't work.
My classes are in One so specifying One using -cp didn't work either.
You'd run it as:
java One.Test
... but from the root directory (basic), not from the One directory. You always specify the fully-qualified class name.
Oh, and package names in Java should be lower-case, so it should be one and one.inner, not One and One.Inner. Just a convention, but one which pretty much everyone follows.
If the directory is:
basic\One
Run java from the base directory of the package:
basic>java One.test or basic>One.test <optional arguments>
(ideally the package would be lowercase and the class upper case):
basic>java one.Test
If you get 'does not exist' messages, then the java command cannot find classes you referenced in your class. You can point to them with the -cp option ('.' means 'here', and you can add as many places as you like divided by ';' on Windows and ':' on Linux).
basic>java -cp . one.Test
or
basic>java -cp .;..\..\someJar.jar;c:\someDirectory\classesDirectory one.Test
The following line of Haralan Dobrev code solves the problem.
java -cp ../ one.Test
while creating a class with a package if you want to run it from cmd you must created a directory with same name of package put the .class in it and then you can easily run it for example you created a class with name "one" and this class in the package with name pack ,you must run these commands
1 javac one.java
after compilation created a directory with the name pack ,after that run this command
2 java pack.one
Note:
all this must be done in the current working directory and the name "one" i chose it here as file name and main class name
we all know the first name used in the first command is file name and second one is main class name
This is because if you are declaring package in your java file, then JAVA compiler believe you are having same folder architecture in your system.
In your case Java compiler looking for One as a package and then test.class., or to be very specific just look inside your .class file you can see what path it looking for. Please have a look for below Image (I my case I use Hello and Tester)
As you can see path in image is Hello/Tester(my case example), so architecture should be like Hello->Tester.
And if you are not having same architecture and want to create same while compiling, then use javacp command.
I have a Java program called Main.java, it is located in the following directory:
/home/user/program/Main.java
When I try to run Main.java from the 'program' directory, everything goes ok, I use this line:
/home/user/program$ java Main
But when I try to run Main.java from the home directory :
/home$ java /home/user/program/Main
I get :
Exception in thread "main" java.lang.NoClassDefFoundError: /home/user/program/Main
Caused by: java.lang.ClassNotFoundException: .home.user.program.Main
What is the cause of this error?
This is due to your classpath, which will default to the current directory. When you run java Main from /home/user/program it finds the class in the current directory (since the package seems to be unset, meaning it is the default). Hence, it finds the class in /home/user/program/Main.class.
Running java /home/user/program/Main from /home tries to find the class in the classpath (the current directory) which will look in /home/home/user/program expecting to find the file Main.class containing a definition of the Main class with package .home.user.program.
Extra detail: I think the java
launcher is trying to be nice by
converting /-notation for a classname
to the .-notation; and when you run
java /home/user/program/Main it is
actually running java
.home.user.program.Main for you. This
is because you shouldn't be specifying
a file, but a fully specified
classname (ie including package
specifier). And when a class has a package
java expects to find that class within a
directory structure that matches the package
name, inside a directory (or jar) in the
classpath; hence, it will try to look in
/home/home/user/program for the class file
You can fix it by specifying your classpath with -cp or -classpath:
java -cp /home/user/program Main
Because its looking for the class using the fullname you give (/home/user/program/Main). You should only look for the Main class but using the good classpath :
java Main -cp /home/user/program
Which means it'll search the Main class in the given set of paths
Your 2nd command version does not know where to find the classes.
You need to provide the so called classpath
/home$ java -cp userprogram Main
Because of what you say I conclude this:
Main is in "top" (root) package
And when you execute java you must indicate the classpath, it is, the root directory where your pakage and classes structure is located.
In your case it is the very /home/user/program. And I guess your classpath is defined as "." (the dir you are located at). When you call java from home the classpath is being taken erroneosly.
If you want to call your main using a different package declare the package at the top of the class:
package user.program;
And set the classpath to /home (or execute java from that dir).
Next call java this way:
java user.program.Main
using dots because its a full class name (indicating packages). That is translated to dirs concatenating classpath + package + class. By example:
/home
user.program -> user/program/
Main -> Main.class
Good luck!
The problem is that if you call java /home/user/program/Main the package Main is in is meant to be home.user.program, which I assume is not true for Main (I assume it's in the default package, i.e. none at all). Is there a package declaration at the top of Main?
I'd suggest to use the classpath suggestions in the other answers.
This works for me:
java -cp /home/user/program Main
just a while ago faced this kind of error of (NoClassDefFoundError). I imported some third party library in my android app using eclipse env. I got this error during a runtime - some class from this third party library couldn't be found and a result of this NoClassDefFoundError was thrown, despite the mentioned library correctly appeared in classpath, so I really didn't know what else can be done to solve this problem.
While playing with "Order and Export" tab within "Java Build Path", I put my imported third party library to the top of the list of all libraries in my project and checked its checkbox - this solved the problem
I came across this same error when trying to compile and run it. The book, "Head First Java" explains and addresses this problem appropriately. Here is a screenshot from the book for your reference.
Hope its helpful.