如何在java中更改user.dir(或命令提示符中的当前目录)?

时间:2023-01-16 23:07:26

When I go to a directory and make a jar file in command prompt, it works correctly. But, when I am in dir1 and make a jar file from dir2, it makes the jar file in dir1 and the jar file has error :

当我转到目录并在命令提示符下创建一个jar文件时,它可以正常工作。但是,当我在dir1并从dir2制作一个jar文件时,它会使dir1中的jar文件和jar文件出错:

couldn't find main class.

找不到主要班级。

I thought it works if I change current directory by java code. If it works from command prompt, it will be correct in java code.

如果我用java代码更改当前目录,我认为它有效。如果它在命令提示符下工作,它在java代码中是正确的。

C:\Users\username\Desktop>jar cfe Main.jar Main F:\Java\Files\Main.class



C:\Users\username\Desktop>java -jar F:\Java\Files\Main.jar
Error: Could not find or load main class Main

1 个解决方案

#1


1  

You are specifying that the Main.jar be generated in the current directory. Include the full path of the jar file when you are generating it:

您指定在当前目录中生成Main.jar。生成时包括jar文件的完整路径:

C:\Users\username\Desktop>jar cfe F:\Java\Files\Main.jar Main F:\Java\Files\Main.class

#1


1  

You are specifying that the Main.jar be generated in the current directory. Include the full path of the jar file when you are generating it:

您指定在当前目录中生成Main.jar。生成时包括jar文件的完整路径:

C:\Users\username\Desktop>jar cfe F:\Java\Files\Main.jar Main F:\Java\Files\Main.class