简体   繁体   中英

How to use java .class file in Visual Studio Code?

I'm currently studying a Java course, and one of the assignments requires me to use (import) a class inside a.class file.

I'm currently using Visual Studio Code, and I can't seem to find a way to import the class into my Java code.

We are not given the.java code. From what I can tell, Visual Studio Code does not support the use of.class files.

as I know, intellij idea can decompile the.class file depends on the build in decompile plugin, while the extension named 'Java Decompiler' in vscode marketplace works a little complex. maybe you can take a try, use the 'Java Decompiler' extension or take a look in the intellij idea.

The easiest way is to have both classes in the same directory, like in this example. Say you have class A

public class A {
    public int fun() {
        return 0;
    }
}

to use class A from class B in the same directory, you just declare a type A in your B

class B {
    public static void main(String[] args) {
        A a = new A();
        int i = a.fun();
        System.out.println(i);
    }
}

this will print 0 .

This works because both classes are in the same directory, and class A is declared public.

Being in the same directory, with no package declaration, means that both classes are in the default package, this is the easiest case.

You may use the extension https://marketplace.visualstudio.com/items?itemName=dgileadi.java-decompiler . The ext can help you view the original source code of.class file.

On the other hand, what you talked is auto-import feature, which is suported by vscode now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM