I have two class like so:
package one;
public class Parent
{
protected String name = "hello world";
}
And:
package two;
import one.Parent;
class Child extends Parent
{
public void testMethod()
{
System.out.println("name is " + name);
}
public static void main(String args[])
{
Child n = new Child();
n.testMethod();
}
}
This classes are in the c\source folder
When I compile this classes in command line I get lot's of error I use more statement like so:
C:\source>javac -d Child.java
c:\sorce> javac *.java
Still I get many errors.
How do I solve this problem?
I do a simple test. you can try again
the current directory structure.
--file
--bin
--Parent.java
--Child.java
then execute the following cmd
cd bin
javac -d ./ ../Parent.java
javac -d ./ ../Child.java
java two.Child
The result is ok
if your code has a package name, you should use the option -d, in the compile process, javac will create directories according to the package name.
for javac tool
please refer
http://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/win32/javac.html
help that helped.
Related
My file structure. I have 2 directories next to each other. module1 and module2.
module1:
src\hu\package1\TestPackagePrivateClass.java:
package hu.package1;
abstract class TestPackagePrivateClass { }
src\hu\package1\PackagePrivateClass2.java:
package hu.package1;
abstract class PackagePrivateClass2 { }
src\hu\package1\TestPublicClass.java:
package hu.package1;
public abstract class TestPublicClass { }
So 2 abstract package-private classes and 1 pubic abstract class.
module2:
src\hu\package1\TestChildClass.java:
package hu.package1;
public abstract class TestChildClass extends TestPackagePrivateClass {
public PackagePrivateClass2 getPackagePrivateClass2() { return null; }
public TestPublicClass getTestPublicClass() { return null; }
}
src\hu\package1\TestChildClass2.java:
package hu.package1;
public abstract class TestChildClass2 extends TestPublicClass {
}
src\hu\package2\ThirdChildClass.java:
package hu.package2;
import hu.package1.TestChildClass;
public class ThirdChildClass extends TestChildClass { }
So I run the following 2 commands... The first 2 from module1 and the second 2 from module2:
javac.exe -d .\build -target 1.8 src\hu\package1\*.java
javadoc.exe hu.package1 -sourcepath .\src -d .\doc -private
javac.exe -d .\build -target 1.8 -cp ..\module1\build src\hu\package1\*.java src\hu\package2\*.java
javadoc.exe hu.package1 hu.package2 -sourcepath .\src -cp ..\module1\build -d .\doc -private -link ..\..\module1\doc
Everything works perfectly fine, no warnings, no errors.
However my problem is that in the generated javadoc for module2 specifically for TestChildClass it shows that it extends from Object, and not from TestPackagePrivateClass. However the return type of getPackagePrivateClass2 is perfectly shown. Note that for TestChildClass2 which extends TestPublicClass from module1, the parent class is sown correct.
So it seems that javadoc is not displaying the parent class when it comes from another module and its not public. Is there a solution for this?
Good morning, everybody.
So, I'm trying to compile a very super-simple Java program by way of a package structure, in which I have two java files. My folder structure is:
Prac/Java/LL/people/src/coreservlets/
and the two files are PersonTest.java (which is the main one) and Person.java
The PersonTest.java file goes as follows:
package coreservlets;
public class PersonTest {
public static void main (String[] args) {
Person p = new Person("Jane", "Smith");
}
}
and Person.java goes as follows:
package coreservlets;
public class Person {
public String firstName, lastName;
public Person(String initialFirstName,
String initialLastName) {
this.firstName = initialFirstName;
this.lastName = initialLastName;
}
public String getFullName() {
return(firstName + " " + lastName);
}
}
And when I try to compile, it tells me that it can't find the symbol Person. I've checked multiple forum entries to check to make sure I'm doing package structure correctly, and I can't figure out what the problem is. What am I doing wrong?
The issue was that the class path needs to be set for each command (javac and java):
Follow bellow Steps
instead of going to subpackage, compile Person.java from the top_level:
$javac -cp . Prac/Java/LL/people/src/coreservlets/Person.java
compile PersonTest.java in the same way:
$javac -cp . Prac/Java/LL/people/src/coreservlets/PersonTest.java
run the file using the class path also:
$java -cp . Prac/Java/LL/people/src/coreservlets/PersonTest
ref : http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html
Can't find a solution to this problem.
Guitar2.java:
public class Guitar2
{
private String serialNumber;
public Guitar2(String serialNumber)
{
this.serialNumber = serialNumber;
}
public String getSerialNumber()
{
return serialNumber;
}
}
Inv2.java:
import java.util.List;
import java.util.LinkedList;
public class Inv2
{
private List guitars;
public Inv2()
{
guitars = new LinkedList();
}
public void addGuitar(String serialNumber)
{
Guitar2 guitar = new Guitar2(serialNumber);
guitars.add(guitar);
}
}
Both files are in the same directory, both are 755 and the directory is in the classpath. I get the error message:
[machine]me # directory $ javac Inventory.java
Inventory.java:18: cannot find symbol
symbol : class Guitar
location: class Inventory
Guitar guitar = new Guitar();
^
Inventory.java:18: cannot find symbol
symbol : class Guitar
location: class Inventory
Guitar guitar = new Guitar();
^
2 errors
I read that if a file is in the same directory, classes from it can be used in other files in the same directory without any import statements. What's the problem here?
POST EDIT Output when using the above code:
[me]machine # ricks $ javac Inv2.java
Note: Inv2.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
I get the .class files of both .java files.
Run javac Guitar.java
and only then (after it has been compiled and a Guitar.class was created) run javac Inventory.java
Try to compile Guitar.java first. then run your cmd . or try this : javac *.java
Make sure both classes have the same package. Even if they are in the same directory, a different package could cause this problem. Type the same package or use an import and then try compiling with javac *.java
I have in one folder /projects/A/Test.java
with the following code:
public class Test {
public static void t() {
System.out.println("Hey");
}
}
and in folder#2 /projects/
with the following code:
public class Test2 {
public final static void main(String[] args) {
Test t = new Test();
t.t();
}
}
I compile the first one with: javac Test.java
I compile the second one with javac Test2.java -cp ./A
Now when I try to run Test2: java Test2, I receive an error:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
How can I run Test2, which calls Test from a subfolder?
If your classes are in different places you have to set the classpath when running, just like when you compile.
java -cp .:./A Test2 # On Unix use :, on Windows ;
The problem is that in class test you had to specify package in the beginning of the file.
package A;
And then import it in your Test2 class.
import A.Test
you don't have to set package and import Test if both locations project and project/A are in your classpath
I've problem compiling these java files .I have a class MeterMovementService.java and an interface MeterMovementServiceMBean.java . The class is implementing this interface . And i compiled the interface first .Both the class and interface resides in same package . But when I try to compile the class it gives error as :
MeterMovementService.java:2: error: cannot find symbol
public class MeterMovementService implements MeterMovementServiceMBean
^
symbol: class MeterMovementServiceMBean
1 error
Here is the code for the MeterMovementService.java class
My class does'nt have dependency.It just implements the interface.
public class MeterMovementService implements MeterMovementServiceMBean {
private String message = "Sorry no message today";
public String getMessage(){
return message;
}
public void setMessage(String message){
this.message = message;
}
public void printMessage(){
System.out.println(message);
}
public void start() throws Exception{
System.out.println(">>>>Starting with message=" + message);
}
public void stop() throws Exception{
System.out.println(">>>>Stopping with message=" + message);
}
}
You need to ensure that your class files are placed under the package folder. For e.g. if your interface is defined under package - pkg1, your class file should be under pkg1 subfolder.
Your directory structure should be like this:
srcfolder -+
+- pkg1 -+- MeterMovementServiceMBean.class
| +- MeterMovementService.class
|
+- MeterMovementService.java
+- MeterMovementServiceMBean.java
Either you have to move the class files manually, or even better, you can compile your .java files using the below command, to let the compiler handle it all for you:
javac -d . MeterMovementServiceMBean.java
javac -d . *.java compiles all the java files in the current directory and packages the compiled classfiles according to their package structure .