| |

VerySource

 Forgot password?
 Register
Search
Author: xenon1

How to call an unpackaged class under a packaged class

  [Copy link]

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 09:45:01
| Show all posts
Top down
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 10:00:01
| Show all posts
[Quote=Quote the reply of独舞明夕on the 4th floor:]
And when the code is placed in a class under the package, it always fails to compile.

You can simply do
1 Put those classes without packages on the root,
2 Your classes are stored in the package directory
3 Just keep up and run

/Company.class
/com/mycompany/MyCom.class

that's it

If you are running in Eclipse, please refer to the practice on the 6th floor and add the path of the class to the build path.
[Java Build Path] [Libraries] [Add Class Folder]

Of course, it’s better to package it into a jar
But the last step is changed to [Add Li...
[/Quote]
.......... The original host foundation is very important............
Reply

Use magic Report

0

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 10:15:01
| Show all posts
Shouldn't it work?
Comrades, there is no way to directly call a packageless class with a packaged class, because the packageless class is placed in a hidden namespace.
I don't know if it can be called by reflection.
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-29 10:30:01
| Show all posts
JAR is the best! ! !
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 10:45:01
| Show all posts
Upstairs said it can be done, I wonder if I have tried it myself
I have no way to call the classes in the anonymous package in my own package like normal
But if it is a simple call, you can try to implement a Class Loader yourself, which will
The class loading in the anonymous package is instantiated into one using the Java reflect mechanism
java.lang.Object object, and then use the reflect mechanism to call the method of this object.

Disclaimer: I have not experimented with this method, it is estimated that it will work, the poster is interested.
Give it a try.

If anyone has a way to access the classes in the anonymous package as usual, please click here
Reply, I am also worried about this time :-(
Reply

Use magic Report

0

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 11:00:02
| Show all posts
Sure enough, you can use reflection. But it’s super troublesome, you might as well write it in the package-less category.

Suppose there is a packageless class that needs to be called:


[code=Java]public class Main {
    public void hello() {
        System.out.println("hello");
    }
}[/code]


Use reflection to call the hello() method of this packageless class:


[code=Java]package hello;

import java.lang.reflect.*;

public class Hello {
    public static void main(String[] args) throws Exception {
        Class clazz = Class.forName("Main");
        Object o = clazz.newInstance();
        Method m = clazz.getDeclaredMethod("hello");
        m.invoke(o);
    }
}[/code]
Reply

Use magic Report

0

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 11:15:01
| Show all posts
Don't worry, it's because java's security mechanism prevents mixed use of classes.
Before jdk-1.3, a special reference method of defaultnamespace was allowed to call classes without packages.
But this usage was immediately cancelled, it is probably too disgusting to look at it.

If it is allowed to use packageless classes directly, how to write import?
You think. If a class without a package can be called directly, it can be used directly without writing import, because it does not have a package name.

For people like me who do tests and write Test.java all over, once the classpath is set up, jvm knows which Test.java to use? You have nowhere to debug if you want to find a mistake.

In this case, reflection is a tricky trick. You should go to the manufacturer to ask for a packaged class. With reflection, if the manufacturer updates the API later, you will be very troublesome. All parts must be rewritten.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 11:30:01
| Show all posts
If the compilation fails, the path of the *.class file is wrong. Organize the package structure
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-8-29 12:00:01
| Show all posts
classpath,classpath,classpath
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-8-29 12:15:01
| Show all posts
Make a directory where you want to put it, then put all the manufacturer’s classes in this directory, and then add this directory to the classpath.
If you are using eclipse, there is also an add folder option to add!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list