I just know how to create a package just by declaring it at the top of the class but I dont know the code for creating it
package myPackage;
class test{
system.out.println("Hello every one");
}
please provide me the code to compile it
This should be your lesson of the day :)
http://download.oracle.com/javase/tutorial/java/package/packages.html
Read from end to end, and don't forget this page http://download.oracle.com/javase/tutorial/java/package/managingfiles.html
Read these for compiling options:
http://www.cs.princeton.edu/courses/archive/fall97/cs461/jdkdocs/tooldocs/win32/javac.html
http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/javac.html
A package is 'created' by the folder hierarchy it is in. For example, your test class is in the myPackage package, which has to be in a folder called myPackage.
If your classes are in packages, then Java expects your source files to be in a directory structure that looks like the package structure. In this case, make a directory called myPackage and put test.java in that directory. Then from the directory the myPackage directory is in, run javac myPackage/test.java. This will create a test.class file in the myPackage directory. If test.java had a main function defined in it, you could then, from the same directory, run java myProject.test.
The 'test' class is supposed to be public, and put it under folder "myPackage". The folder is package.
Related
I want to write a code like this
package mypackage;
public class A extends B {
}
But all that I have is the B.class file which is compiled from a single B.java file with no package specified.
Could anyone help me out?
Thanks!
I've tried putting B.class in my ./src and A.java in ./src/mypackage and run javac -cp src ./src/package/A.java but it wouldn't compile. It wouldn't compile neither if I put B.class in the same folder as A.java
You can't. By your description, class B is in the default (unnamed) package. The Java language rules do not allow classes outside the default package to refer to classes in the default package.
The only way you can use this class is to put A in the default package as well. That is, move it one directory up, and remove the package statement.
Alternatively, you need to have the sources of class B (or decompile it), move it into a package and add a package statement, and recompile.
I am a Java newbie, just started learning it in college, and my class is using NetBeans but I'd like to use VSCode.
The professor told us that every Java file should start with:
package nameofthepackage
So that Java knows to which package the class (file) I created belongs to.
So I always create this structure:
I create a folder with the name of the main class, and inside this folder I make a src folder that will store the Java files. Eg:
MyJavaProject/src/MyJavaProject.java
I always name the main .Java file the same as the project folder name.
And when I compile I use javac with the -cp parameter to specify that the src folder is the classpath folder, where it should look for the .Java files I create.
I also always tell javac to compile all the files inside the src folder, using the * wildcard.
The issue is that with this line on top of my .Java files, javac compiles all the files, but I can't execute the bytecode because it complains it can't find the classes I created, even the main one.
As Soon as I remove the package line from the top of the files, I can compile and run the code.
So far it's good, but for any more complex projects this is gonna be really annoying.
Any ideas how can I fix this?
You need to specify the full name (with package) of the class that contains the main function.
Assuming the class MyJavaProject is the one containing your main, that would be:
java nameofthepackage.MyJavaProject
This is assuming you built it with
javac -d . MyJavaProject.java
It would create your target class in a directory structure like:
nameofthepackage\MyJavaProject.class
I am trying to implement an interface in my java code as such:
package PJ1;
public class Fraction implements FractionInterface, Comparable<Fraction>{
Now, FractionInterface.class in the same directory as the Fraction.java file, and it is also in package PJ1:
package PJ1;
public interface FractionInterface{
Yet when I try to compile my Fraction.java file, I get the following error:
D:\CSC220\PJ1\Fraction.java:36: error: cannot find symbol
public class Fraction implements FractionInterface, Comparable<Fraction>
^
I'm stumped, since all of my related files are in the same directory and I'm trying to put all of my class files in the same package. Any ideas?
try to compile like this:
e.g. in c: you do have both the java files - Fraction.java and FractionInterface.java , and you have not created any folder for packages yet, then try as:
c:> javac -d . *.java
This will compile all the files with creating required packages. You no need to create any folders for packages manually.
If you already have created the folder for packages, and you are already in the package say:
c:\PJ1, you can simply compile using javac as:
c:\PJ1> javac *.java
Hope this will work.
My guess is that the files are not in a directory called PJ1 relative to where the compiler expects them to be. Create the folder and move both files to that location. To make it a bit clearer, let's say your folder structure looks like this
myfolder
+-PJ1
Fraction.java
FractionInterface.java
Then you need to be compiling from myfolder using
javac PJ1\Fraction.java
Be sure that both files are in PJ1 folder and run javac *.java.
Just go to a directory above the directory in which your files exist and compile your java files from there (so that you can see if they compile correctly), e.g.
javac dir1\file.java.
I am a newbie in java I Want to know that what is the default directory for packages in java ? I Mean if i compile a java file which contains a package statement,and i compile it without using -d option in javac command,then where will be the package created ? eg.
package test;
class Test{}
and compile it using javac Test.java
then where will be the package created?
Thanks
If you don't specify -d, the class file will be created in the same directory as the source file.
That's fine if you're already storing your source in a directory structure matching your package structure (and if you're happy for your source and class files to live in the same place) but if your source structure doesn't match your package structure, you'll basically end up with class files in locations where they can't sensibly be used.
Personally for anything other than quick throwaway (usually Stack Overflow :) code I would make the following suggestions:
Avoid using the default package
Keep your source code in a directory structure (e.g. with a root of src) matching package structure
Generate class files into a separate directory structure (e.g. with a root of bin or out or classes)
(Sorry, misread the question to start with.)
-d option in javac command is use to specify where to generate the class file,if you don't specify it,then the .class file will be created in the same directory where your .java file is present.
There is no directory created when you do not specify the package!
all the .class files will be created directly in the output folder
public class A{}
if you compile this to output folder ,
output/a.class is created
I want a Java application to have a directory structure like the following (like maven):
src
main
java
org
example
When I add a new Java class called NewClass using the Netbeans IDE, it creates the package statement in NewClass.java as package main.java.org.example;.
If I remove main.java. from the package statement I get the error Incorrect package.
I do not want main.java. in the package statement, but I do want my directory hierarchy as mentioned above.
How do I get the package naming and the directory structure to play along?
You need to change your source directory from /src to /src/main/java