| |

VerySource

 Forgot password?
 Register
Search
Author: xenon1

How to call an unpackaged class under a packaged class

  [Copy link]

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-29 12:30:01
| Show all posts
Bangdingxia
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-29 12:45:01
| Show all posts
You should go to the vendor to ask for a packaged class. Use reflection. If the vendor updates the API in the future, you will be in trouble. All parts must be rewritten.



Support, this is the root!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 13:00:01
| Show all posts
Learn it!
Reply

Use magic Report

0

Threads

4

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 13: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.
Use reflection to call it.

Suppose there is a non-package 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

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 13:30:01
| Show all posts
Hit the jar package!
Just import the jar package you built in the directory of your original project to pull it
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 14:00:01
| Show all posts
Refer to how to reference other packages in eclipse. Just apply it in the build path of the project.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-29 14:15:01
| Show all posts
So JAVA does not recommend packageless classes
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 LAN

Post time: 2020-8-29 14:45:01
| Show all posts
Study, hey
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