使用来自jar文件的类在VSCode中

huangapple go评论78阅读模式
英文:

Use a class from a jar file in VSCode

问题

import java.io.*;

public class Ler {
    public static String umaString (){
        String s = "";
        try{
            BufferedReader in = new BufferedReader ( new InputStreamReader (System.in));
            s= in.readLine();
        }
        catch (IOException e){
            System.out.println("Erro ao ler fluxo de entrada.");
        }
        return s;
    }

    public static int umInt(){
        while(true){
            try{
                return Integer.valueOf(umaString().trim()).intValue();
            }
            catch(Exception e){
                System.out.println("Não é um inteiro válido!!!");
            }
        }
    }
    
    public static char umChar(){
        while(true){
            try{
                return umaString().charAt(0);
            }
            catch(Exception e){
                System.out.println("Não é um char válido!!!");
            }
        }
    }
    
    public static short umShort(){
        while(true){
            try{
                return Short.valueOf(umaString().trim()).shortValue();
            }
            catch(Exception e){
                System.out.println("Não é um short válido!!!");
            }
        }
    }        
}

PS: 若你有关于这段代码的问题或者其他需要讨论的内容,欢迎随时提问。

英文:

So, I'm new to Java, and I was looking at how to use a class from a Jar file. I created a java file with all the classes I wanted and then got a Jar file from it, I imported it using this interface but now how can I actually use it? I'm not using Maven or anything. Just plain java project from VSCode (Java Extension Pack and Jar Builder installed).

I know that maybe I should use a different program but I really like VSCode so If u could help here I would appreciate it 使用来自jar文件的类在VSCode中

Here is the java file that I built the Jar from

import java.io.*;
public class Ler {
public static String umaString (){
String s = "";
try{
BufferedReader in = new BufferedReader ( new InputStreamReader (System.in));
s= in.readLine();
}
catch (IOException e){
System.out.println("Erro ao ler fluxo de entrada.");
}
return s;
}
public static int umInt(){
while(true){
try{
return Integer.valueOf(umaString().trim()).intValue();
}
catch(Exception e){
System.out.println("Não é um inteiro válido!!!");
}
}
}
public static char umChar(){
while(true){
try{
return umaString().charAt(0);
}
catch(Exception e){
System.out.println("Não é um char válido!!!");
}
}
}
public static short umShort(){
while(true){
try{
return Short.valueOf(umaString().trim()).shortValue();
}
catch(Exception e){
System.out.println("Não é um short válido!!!");
}
}
}        
}

答案1

得分: 1

以下是您要求的翻译内容:

您可以通过以下答案中提供的提示来验证您的工作:https://stackoverflow.com/a/54535301/14056755

当您将该库导入普通的Java项目时,您只需使用import关键字从jar库中调用类。您在jar库中的类应该放置在一个包内。

示例(假设Ler类位于org.xyz包内):

import org.xyz.Ler;
英文:

You can verify your work with the tips written in the answer to similar problem here: https://stackoverflow.com/a/54535301/14056755

When you import the library into your plain Java project, you can just summon classes from jar library by using import keyword. Your class inside jar library should be placed inside a package.

Example (assuming Ler class is placed inside org.xyz package):

import org.xyz.Ler;

答案2

得分: 1

这是解决方案:

  1. 使用命令 javac Ler.java 编译 Ler.java

  2. 输入命令 jar --create --file Ler.jar Ler.class 生成 Ler.jar

注意:如果 Ler.java 在子文件夹中,请进入该文件夹后再进行编译和生成 .jar 文件。

  1. Ler.jar 添加到 Referenced Libraries(引用的库);

  2. 像这样编写代码:Ler.function()。

英文:

Here's the solution:

1.Use the command javac Ler.java to compile Ler.java;

2.Type the command jar --create --file Ler.jar Ler.class to generate Ler.jar;

>Attention: If Ler.java is under the subfolder, please enter it then compile and generate .jar.

使用来自jar文件的类在VSCode中

3.Add Ler.jar to Referenced Libraries;

4.Code like Ler.function().

使用来自jar文件的类在VSCode中

huangapple
  • 本文由 发表于 2020年10月15日 01:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64358527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定