compiling a class with a package throws an error - java

I'm fairly new to java and am trying to get the basics down. One of them would be dealing with package, I'm trying to implement one class into another using package HelpPack; and upon using javac -d HelpPackage A.java it throws the error javac: file not found: A.java. Thank you for your help
//B.java
package HelpPack;
public class B{
public String name(){
return "Class b";
}
}
//A.java
package HelpPack;
public class A{
public static void main(String[] args){
B b = new B();
System.out.println(b.name());
}
}

Compile all your classes
javac -d . *.java
and run using,
java HelpPack.A
Have a look at javac tool
From the tool doc : By default, the compiler puts each class file in the same directory as
its source file. You can specify a separate destination directory with
-d (see Options, below).
and package tutorial.

-d specifies where the class files will go. Your java files need to be in that directory too and command should be
javac HelpPack/*java

Your package needs to match an actual directory.
So your class A.java should be in the directory HelpPack.
According to documentation :
-d directory
Set the destination directory for class files. The directory must already exist; javac will not create it. If a class is part of apackage, 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.
If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.
Note: The directory specified by -d is not automatically added to your user class path.

Related

How to compile and run java with package

HI I'm having trouble with my java compille
I made folder named 'Test'. In this folder i make two folders, one is src, another one is bin. Then I made Test. java in that src folder
package Test;
import java.io.*;
public class Test {
public static void main(String args[]) {
System.out.println("hi");
}
}
i saved it and back at Test folder and then i compile like this
javac -d bin src/Test.java
Thus, i have Test folder in bin folder.
finally in Test folder i write this command
java -cp bin/Test Test
unfortunately, it says can't find Test class
How can i run this code???
When your class is in a package, the name of the class includes the package. Thus Test.Test is the Test class in the Test package. -cp bin tells java that the classpath starts in bin.
java -cp bin Test.Test
# classpath main-class
"-cp" expects a directory, not a file. Give it the ./bin/ directory, not the file you're trying to execute.
java -cp bin Test.Test

Java classpath: Bad class file: contains wrong class

I have the following project structure:
MyProject
+-Main.java
+-package1
+-ClassA.java
+-package2
+-ClassB.java
+-ClassC.java
After compiling using javac *.java from the MyProject folder, the .class files are created in the folders where the corresponding .java files are located.
This is an overview of my Main.java file:
import package1.*;
import package1.package2.*;
class Main {
public static void main(String[] args) {
ClassB test = new ClassB();
}
}
And this is the error I get when compiling:
Main.java:15: error: cannot access ClassB
ClassB test = new ClassB();
^
bad class file: ./package1/package2/ClassB.class
class file contains wrong class: package2.ClassB
Please remove or make sure it appears in the correct subdirectory of the classpath.
I have made sure that the package structure matches the folders.
In addition, I tried compiling the project manually by using
javac -cp package1/package2 package1/package2/*.java
javac -cp package1 package1/*.java
javac -cp . Main.java
Which unfortunately produces the same error.
My verdict is that this issue is caused by the location of the .class files in package2, which the compiler can't seem to handle properly but I'm not sure how to modify my project structure.

couldn't find or load main class

I placed my Java code to the binfolder and try to run the code. The command javac Project.java terminated successfully, but the command java Project throws the error
couldn't find or load main class Project
This is my code:
Public class project {
public static void main(String args[]) {
}
}
You've got your error because of wrong syntax. Of course, java can't find Project class, there's no such thing declared. This is a correct declaration:
public class Project {
public static void main(String args[]) {
System.out.println(args[0]);
}
}
Note, that in Java class names start with upper-case letter, and access modifiers - with lower, like public, private, etc. I strongly suggest you to read Java Naming Conventions before writing any code.
If you're getting error like
couldn't find or load main class Project
there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions ), so you need to put in on the classpath with -cp option (as it mentioned by #Nikhil B). Note, that doing
javac -classpath "c:\java\jdk1.7.0.45"\bin" Project.java
which you posted in comments to his answer isn't correct. You should tell java interpreter where to find .class files, not java compiler (+ as I see, you've compiled your .java file just fine).
So, put the directory which contains .class file to a classpath somehow like this:
[root#crmdev clarify]# pwd //shows current directory
/home/clarify
[root#crmdev clarify]# javac Project.java //compiles .java file
[root#crmdev clarify]# ls Project.* //here are my test files for your case
Project.class Project.java
[root#crmdev clarify]# java -cp . Project "hello, #user5779261" //executing test code
hello, #user5779261
Run the java command with classpath option and it should run. (and change the class name to project from Project)
java -classpath "path to bin directory in double quotes" project

Error java executing: Couldn't find or load the main class

I'm trying to execute a simple java code (I have already compiled it with no problems) but it gives me the next error:
c:\Users\alejandro\Desktop> java HelloWorld.java
Error: Couldn't find or load the main class.
The code is the next:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world!");
}
}
I have set the PATH variable correctly.
I have deleted CLASSPATH variable.
I have both files (.java and .class) in my Desktop.
You're specifying the name of the source file. That's not what you provide to the java command - you specify the class name.
java HelloWorld
This assumes that HelloWorld.class is somewhere on the classpath, which would default to "the current directory".
If you had a package, e.g.
package foo;
public class HelloWorld {
...
}
Then you would want to put HelloWorld.java in a directory called foo, and compile and run from the root directory:
> javac foo\HelloWorld.java
> java foo.HelloWorld
Note how now the fully-qualified class name is foo.HelloWorld, not foo\HelloWorld.
when you run the compiled file, you should use only the class name. The compiled file will have an extension of .class but you should not add any extension. Just use the class name.
change
c:\Users\alejandro\Desktop> java HelloWorld.java
to
c:\Users\alejandro\Desktop> java HelloWorld

Does the javac command automatically create directories specified in the package declaration?

I've always used an IDE when working with Java so my knowledge on the javac command isn't that great. I want to know this: Does java generate the directories where the .class files should be placed in as specified in the .java files package declarations? Let me clarify, say you have a simple .java file like this on your Desktop:
package com.deangrobler.test
public class Test {
// ...
}
When running the following from your Desktop:
javac Test.java
Will it then automatically go and create the com/deangrobler/test directories and place the Test.class file therein?
From the docs --> http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
See the option section.
-d directory
Set the destination directory for class files. The directory must already exist; javac will not create it. 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.
If -d is not specified, javac puts each class files in the same directory as the source file from which it was generated.
Note: The directory specified by -d is not automatically added to your user class path.
Will it then automatically go and create the com/deangrobler/test
directories and place the Test.class file therein?
No. Even if you can use -d to specify where you want to place your class file created after compilation but java won't create package structure for you.
So,Unless you specify the -d option the compiler places each class file in the same directory as the corresponding source file was located.
Even if you have specified package name but you hadn't created that structure don't expect that from javac all the things you have to do manually.
For Example:
javac -d C:/tempFolder MyProgram.java//Will place .class file in to C:/tempFolder
//Only if C:/tempFolder is available
com
|
|---deangrobler
|
|-------test
|
|---Test.class//Places class file here after compilation
//Default place
If we use below command , it will create package structure from current directory.
javac -d . YourClassName.java
If we want to put your class files and its packages some other folder, use below command.
javac -d "d:/java_eg/" YourClassName.java
it will create package directories and class files inside java_eg folder inside the D drive.

Categories