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

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.

Related

Java 11 compiler returns "package does not exist"

I am trying to re-learn some programming skills.
I have decided to use an old favourite - Logic Gates Simulation - as a learning tool.
I want to set this project up to use packages.
My CLASSPATH is "C:\Users\ruthm\Documents\java"
My project code is in the directory:
C:\Users\ruthm\Documents\java\logic
I am using Java 11
My classes so far are Connector.java and ConnectorTest.java
the code for Connector is as follows:
package logic;
/*
Class Connector
A connector forms the input and output of a LogicGate.
The connector "output" object of a gate can be passed to another gate to form the input.
*/
public class Connector{
private int value;
public Connector(){
value=0;
}
public Connector(int state){
value=state;
}
public void setValue(int state){
value=state;
}
public int getValue(){
return value;
}
}
The code for Connector Test is as follows:
/* Test Case for Connector */
import logic.*;
class ConnectorTest{
public static void main (String[] args){
logic.Connector myConnector = new logic.Connector();
System.out.println("initial value: "+myConnector.getValue());
myConnector.setValue(1);
System.out.println("Set value: "+myConnector.getValue());
}
}
Connector.java compiles without error.
When I try to compile ConnectorTest.java I get the following from the compiler:
C:\Users\ruthm\Documents\java\logic>javac ConnectorTest.java
ConnectorTest.java:4: error: package logic does not exist
import logic.*;
^
ConnectorTest.java:9: error: package logic does not exist
logic.Connector myConnector = new logic.Connector();
^
ConnectorTest.java:9: error: package logic does not exist
logic.Connector myConnector = new logic.Connector();
^
3 errors
C:\Users\ruthm\Documents\java\logic>
I have been following guides on directory structure and packages to try and solve this but I am clearly not understanding something.
I get the same errors if I declare ConnectorTest to be in package logic as well.
Can someone handhold me and show me where I am going wrong?
My project code is in the directory:
C:\Users\ruthm\Documents\java\logic
This means that to avoid trouble, you'd be better off using same package directive to them both.
So if you add this:
package logic;
to your ConnectorTest class, it should be ok, given you don't have any other issues.
If you want to keep your ConnectorTest class in default package, you could move your ConnectorTest.java file to C:\Users\ruthm\Documents\java directory, but keep your Connector class in the logic directory.

Netbeans doesnt recognize class in the same package

i am creating a little game with libgdx framework and netbeans 8. I have all java classes in a single package that match with the directory structure.
The problem is that i cant import or isntantiate classes, for example:
package com.myfolder.folder2;
import ...
public class myclass1{
private myclass2 mc2;
etc...
}
In this case myclass2 is public and is inside the package but netbeans complains "cannot find symbol".
If i try with alt+enter, netbeans says "Create class myclass2 in package com.myfolder.folder2" or the same like an inner class. If i press the first option, netbeans create a class in the package with the file name myclass2_1 (becouse myclass2 exists!), and myclass1 doesnt recognize the new class.
If i try to import the class:
import com.myfolder.folder2.myclass2;
It gives me the same error, and in fact the code completion tool only gives me one crazy option in the import sentence:
import com.myfolder.folder2.myclass1;
Import the same class.
What can i do? I never have these problems using netbeans.
PD: Sorry for my english :)
You can use a class inside the same package like this:
ClassName classVariableName = new ClassName();
Then when you want to run something from the class you would put
classVariableName.MethodThatIWantToRun();
Or if you want to access a property from that method you would access it in a very similar way:
classVarabileName.PropertyIWantToAccess
Example:
You have one class with a property you want to access:
class MyClass {
public int MyProperty = 5;
}
You access it in this class:
public class MainClass {
public static void main(String[] args) {
MyClass myClass = new MyClass();
System.out.println(myClass.MyProperty);
}
}
If that doesn't work than you might have some other problem.
It was an error with one of my class package definition:
public class DesktopLauncher{
public static void main(String... args){
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
.
.
.
new LwjglApplication(new MyClass, config);
}
}
It was in MyClass, becouse i copied a snippet from an older project, and accidentally copied the older package.
NetBeans is not smart enough,
Solution: just declare the package name in all classes, example:
Class A:
package test;
public class ClassA {
public static void main(String[ ] args) {
ClassB.myFunctionB();
}
}
Class B:
package test;
public class ClassB {
public static void myFunctionB () {
System.out.print("I am ClassB!");
}
}

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

Running Java Class which calls class from another folder

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

java setting classpath for class implementing interface

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 .

Categories

Resources