I have this program with 3 source files, being pretty new to java I recently learned that I should package these. So I went and did that, I already knew a little about packaging. However, after trying to run the class file with the main method, java could not find/load the main method.
After a while, I finally discovered that the cause was the package line. When the package line is there, the error appears, when the package line isn't there (or is commented out), the program runs fine.
package PeriodicTable;
class PeriodicTable {
public static void main (String[] args) {
//Lines of code
}
}
According to various tutorials and the java doc, all you need to do is put the package line, the package name and a semi-colon at the end. Google searching the error (with package as the cause) did not help me.
I've tried changing the package name, so it was not the same as the class name, this did not work.
What am I using?
Notepad
Command Prompt
Java 8
As for my question... Why is the package line preventing java from finding/loading the main method? How do I fix this?
Go to the package directory, in your case it is PeriodicTable. Run following commands
$ javac -cp . PeriodicTable/PeriodicTable.java
$ java -cp . PeriodicTable.PeriodicTable
Hello
Following link may help you.
Yeah, I've noticed this as well in the past. I just remove the package, since I'm usually calling just a couple programs from the same package anyway at most. :) My mantra is, if it works, take yes for an answer and move on...l
Related
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'm just getting started with Java. This is my program:
package javaapplication1;
public class JavaApplication1
{
public static void main(String[] args)
{
System.out.println("Hello ");
}
}
If I remove the first line package javaapplication1, the code won't run. I did the same thing in class but it was working.
Can someone explain why does this happen?
If you are working with terminal along with package statement, save your code as JavaApplication1.java
and then compile your current code with below syntax.
javac -d . JavaApplication1.java
( -d . indicates create directory in current location because we are using package statement).
then for execution of your code you need to change directory with
cd javaapplication1
then execute your code with
java JavaApplication1
It will execute fine.
But if you are working without package statement seen will differ, you need to compile code normally with
javac JavaApplication1.java
then execute code with
java JavaApplication1
You will not get an error.
Note: But If you are using any IDE nothing to worry about. IDE will take care of package statement.
error: Could not find or load main class: this error appears at runtime if JVM is unable to find main class.
You need to change the directory as I mentioned above, then it might work fine.
This has to do with the way your IDE sets up your project.
When you start learning a new language, it's always a good idea to skip the IDE until it actually becomes useful. I suggest you copy your program to a basic text editor, remove the package line, save it as JavaApplication1.java and manually compile and run with javac JavaApplication1.java and java JavaApplication1.
I built a little command line program in java. It has a class that gets instantiated and used in the main method. But when I go to compile it at the command line it doesn't 'see' the class it needs to run. It accepts arguments passed in at runtime. I can set it up in Netbeans and it runs beautifully. But I want to be able to use it at the command line. I've tried jar-ing it up, it throws an exception and doesn't see the class that I'm instantiating in main. I took Java in my CS program, but my Prof didn't cover deployment in particular depth.
Any ideas to help me out of my pickle?
Thanks!!
Do either of your classes have packages? If they do, they'll have a first statement of "package ", and it makes a difference.
I'm going to assume that at least your Age class does have a package, I'll call the package 'a'.
Let's further assume a main class of "Alex"; it would have an import statement of "import a.age;".
Let's assume you are in a directory named "george".
Your Alex.java file (without a package statement) needs to be in george. Age.java needs to be in a directory underneath george named a.
You can compile your main file with the command "javac Alex", and can run it with "java Alex".
If you tell us more specifics about your problem, we can be more specific about what you need.
Trying to import a class I made in Dr. Java. I made a simple class called QuestionObject which has String questionString and an array for answers and then an int which corresponds to the correct answer and corresponding getters, setters, and constructors. Compiles fine. In my other class file, the one with my main method and such called Game, when I try import QuestionObject; I get a 'cannot resolve' error in the compiler. I saved the class in the same folder as Game.java. Doing everything in OS X not using command line.
Help please! There's probably a simple answer, I just can't find it!
Don't import the class if both classes are in the same directory and in the default package!
Try removing the import statement.
Try setting the package of both classes. And make sure that both classes are in the class path. I am not familiar with Dr Java but with the command line compiler javac *.java in the directory with the classes should pickup both classes.
How do I create a Java package for different files? I have tried
the following. What have I done wrong? And what is the
right procedure?
The first file is:
package dil;
public class Hello
{
Support sp=new Support();
int i=sp.tin();
public void man()
{
System.out.println(i);
}
}
The second file is:
package dil;
class Support
{
public int tin()
{
return 3;
}
}
Now while I compile hello.java it shows these errors:
Hello:4:cannot find symbol
symbol: class Support
location: class dil.hello
Support sp=new Support();
^
Hello:4:cannot find symbol
symbol: class Support
location: class dil.hello
Support sp=new Support();
^
Where is the problem and how can I put both
these files in a package?
The files are in c:\src.
Assuming UNIX / Linux pathnames, a UNIX shell, etc, you need the following file structure:
/some/where/dil
/some/where/dil/hello.java
/some/where/dil/Support.java
Then set $CLASSPATH to /some/where, and compile using the commands
cd /some/where
javac dil/*.java
and run using
java dil.hello
Alternatively, you can tell java and javac what classpath to use with the -cp command line option.
You should also fix the following errors in the code:
Change the name of the "hello" class to "Hello", and rename the source file to match. Strictly speaking this is not an error, but it is a gross violation of the standard for naming Java classes.
You declare a member as "ten" but refer to it as "tin". Fix one or the other.
The entry point method in the "hello" class should be called "main" not "man", and should have a signature public static void main(String[] arg). If you don't fix these the code will compile, but the java command won't find the entry point and will fail.
Although the Support class is not public, that would not be a problem as both classes share the same package. My guess would be that you did not put both source files into a directory according to their packagename and call the javac compiler from the current directory where hello.java resides.
If a class is in package a.b this means the project structure should contain a folder ./a/b containing yourclass.java.
In your case, try to create a folder named ./dil, put your source files in it and call javac from its parent folder.
See Creating and Using Packages in Sun's Java Tutorials to learn all the details of using packages in Java.
I've spotted some things you have to check:
class hello starts with a lower case
class hello calls for sp.ten() instead of sp.tin()
Support isn't public. Make it public and try again.
I suggest you try using one of the free IDEs like Netbeans, Eclipse or IntelliJ CE. This will help you start coding rather than setting everything up the hard way.
BTW: These IDEs have quick fixes for most common problems so they not only give you the error but give you options to fix them and do it for you.