java setting classpath for class implementing interface - java

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 .

Related

How to access a Java class in the default package from jshell?

I am unable to access Java classes in the default package from jshell. I cannot import since the default package has no name and when I try to reference the name then I have an error:
jshell> Test.test(); | Error: | cannot find symbol | symbol:
variable Test | Test.test(); | ^--^
The only solution I found so far is using:
/open Test.java
Is it possible or is it a bug?
Here the code of Test.java:
public class Test {
public static void test() {
System.out.println("test");
}
public static void main(String[] args) {
Test.test();
}
}
This is not possible. From the docs,
Code in the default package, which is also known as the unnamed package, can’t be accessed from JShell.

javadoc generate "extends Object" instead of the real class when the parent class is package private and comes from another module

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?

Run Package base program in command line

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.

package, class and excecutionals files structure

In my Win7 machine I have added in the CLASSPATH like this:
CLASSPATH=D:\Dev\Java;C:\Program Files (x86)\Java\jre1.8.0_20\lib\ext\QTJava.zip.
In my directory tree I have created a D:\Dev\Java\abc folder and placed a filed called Address.java that contained this code:
package jme;
public class NewClass {
}
Having done that, I created a project that looks like this:
package javaapplication1;
package abc; // << Error
public class JavaApplication1 {
public static void main(String[] args) {
abc.Address address; // << Error
System.out.println("Jaaaa");
}
}
Why the abc package, when located in the CLASSPATH, is not recognized?
You need to use import ...
package javaapplication1;
import abc.*; // No error if you have the package in the classpath ...
public class JavaApplication1 {
public static void main(String[] args) {
Address address; // No need to prefix with abc, since you imported it before ...
System.out.println("Jaaaa");
}
}
You can't declare double package for a class in Java, and I think that is not what you really want to do ...
To import correctly the classes contained in the abc package make sure to have the abc package and their related classes in your classpath ...
Sorry guys for the horrendous mishap, I am kinda new here, but I'm a quick learner.
The CLASSPATH reads: D:\Dev\Java\abc;C:\Program Files (x86)\Java\jre1.8.0_20\lib\ext\QTJava.zip

Can't use a Java class in another file

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

Categories

Resources