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.
Related
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 working on a Java project where there is a directory Project which has two sub-directories(GUI and Logic)
Relevant classes:
frame.java(GUI package)
logic.java(Logic package)
The main class is in the GUI package and it imports a file which is in the 'Logic' package.
On trying the below command and running the file, I encounter an `NoClassDefFoundError'.
javac -sourcepath [path to Project] [file containing the import]
I finally could compile it. All the directories and package names were, right, what I needed to do was to use the following command from my root directory:
javac src/gui/*.java
and then, to run:
java -cp ./src gui.Gui
Thanks for the ones who tried to help!
If I right understand your directory structure is like
.\source
.\source\GUI
.\source\GUI\frame.java
.\source\Logic
.\source\Logic\logic.java
Then this will not work, as the direcotry names should be named like the package names. Change the structure and the class names as
.\source\gui
.\source\gui\Frame.java
.\source\logic
.\source\logic\Logic.java
Assuming the import statements and the class names inside the file are correctly amended you can compile it with
javac -sourcepath source source\gui\Frame.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 am using Notepad++ to write my Java code and Command Prompt to compile and run it.
Following is my sample Java code,
package abraKadabra;
public class SuperClass{
protected int anInstance;
public static void main(String [] abc){
System.out.println("Hello");
}
}
However, this file is in the following folder structure :
"usingprotected\superPkg" (usingProtected is a folder somewhere in the hierarchy in C:)
So, my package name here should be something like usingProtected.superPkg instead of abraKadabra as I wrote it.
But, when I compile this Java code from command prompt, it compiles fine with no error or warnings. Why is it so? Shouldn't the package name adhere to the folder structure?
And if it should, how would it adhere?
For e.g. if my package name is usingProtected.superPkg, will the compiler check in the reverse order. The present working directory should be superPkg, then the parent directory should be usingProtected and its done. Is it how it checks the folder structure with package name?
The Java language specification doesn't force files to be in a certain directory. It optionally allows the compiler to require that public classes are in files with the same name of the class, but I don't think there's anything similar for packages. Section 7.2.1 talks about possible storage options in a file system, but it doesn't say anything about enforcing source code structure, as far as I can see.
However, it's best practice - and a pretty much universally accepted convention - to reflect the package structure in the source directory structure... and javac will use this to try to find source files which aren't explicitly specified to be compiled.
Note that if you're compiling from the command line, by default each class will appear in the same location as the corresponding source file, but if you use the "-d" option (e.g. "-d bin") the compiler will build an appropriate output directory structure for you, rooted in the specified directory.
After experimenting a bit, I got the way how to use package name and run Java class files from command prompt.
Suppose following is my Java source file:-
package mySample;
public abstract class Sample{
public static void main(String... a){
System.out.println("Hello ambiguity");
}
}
This file is in directory "D:\Code N Code\CommandLine".
Now, when compile the source code (by going to the above directory from cmd) using following command:-
javac -d . Sample.java
This automatically creates "mySample" folder in my current directory. So, my class file Sample.class is present in directory "D:\Code N Code\CommandLine\mySample". Compiler created this new folder "mySample" from the package name that I gave in my source code.
So if I had given my package name to be "package com.mySample", compiler would create two directories and place my class file in "D:\Code N Code\CommandLine\com\mySample".
Now, I am still in the present working directory i.e. in "D:\Code N Code\CommandLine". And to run my class file, I give the following command:
java mySample.Sample
So, I give the complete hierarchy of package and then the class name. The Java Interpreter will search the current directory for "mySample" directory and in that for "Sample.class". It gets it right and runs it successfully. :)
Now, when I asked that why it compiles my wrong package source code, it would compile the code successfully though, but it gives NoClassDefFoundError when I run my class file. So above method can be used to use package names from command line.
If you're compiling a single class, javac doesn't need to look elsewhere for it. It'll just compile the file as is and put the resulting .class into the same folder. However, you generally won't be able to use the class til you put it into an "abraKadabra" directory in one of the directories in the class path.
If your class uses another class in the package, though, you might have problems compiling it where it is, for the same reason (javac wants to find the class and make sure it has the methods and such that your class uses).
Java compiler does not check the directory structure when it compiles source files. As you mentioned, suppose you have a source file that starts with the directive
package abraKadabra;
You can compile the file even if it is not contained in a subdirectory .../abraKadabra . The source file will compile without errors if it doesn’t depend on other packages. However, the resulting program will not run (unless also including package name in execution). The virtual machine won’t find the resulting classes when you try to run the program.
I am trying to compile and run my java application from the command prompt but am having some errors. I change to the bin bin folder thus am running from the bin folder.I tried compiling with :
>> javac foe.java
but i get some errors that it can't find some classes that are been referred to by the above main class.
Do i have to compile all classes that the above main class references? and if so how do i do it?
thanks.
javac looks in the classpath for those classes referenced by your programs.
If you are unfamiliar with the classpath concept, please see the appropriate Java Tutorial section: http://download.oracle.com/javase/tutorial/essential/environment/paths.html
In foe.java, what are the import statements at the beginning of that file? If you're using classes that aren't part of the standard Java Runtime (classes whose package name begins with java.) such as classes you made, classes that belong in other libraries (jar files), you must add them to your classpath before compiling, otherwise javac won't be able to find them.
The rule is: if it ain't part of the JRE, you have to be explicit when you compile.
if your main file is in c:\path1\mainfile.java
and referenced java file is in c:\path2\reffile.java
from c:\
java -cp c:\path2 c:\path1\mainfile.java
will compile both your files. (i am assuming you are not using any packages)
to run mainfile.java from c:\
java -cp c:\path1;c:\path2 mainfile