英文:
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
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
这是解决方案:
-
使用命令
javac Ler.java
编译Ler.java
; -
输入命令
jar --create --file Ler.jar Ler.class
生成Ler.jar
;
注意:如果
Ler.java
在子文件夹中,请进入该文件夹后再进行编译和生成 .jar 文件。
-
将
Ler.jar
添加到 Referenced Libraries(引用的库); -
像这样编写代码:
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.
3.Add Ler.jar
to Referenced Libraries;
4.Code like Ler.function
().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论