简体   繁体   English

Intellij 和 cmd 的 java 项目的相对路径是否不同?

[英]Is the relative path for a java project different for Intellij and cmd?

I have a java assignment, and at a specific point, we have to create a new folder and write some text files there.我有一个 java 作业,在特定点,我们必须创建一个新文件夹并在其中写入一些文本文件。 The issue is, when testing my code on Intellij, it works fine, but when testing it on cmd, I need to change it a tad bit?问题是,在 Intellij 上测试我的代码时,它工作正常,但是在 cmd 上测试它时,我需要稍微改变一下吗?

My project structure:我的项目结构:

.../project/src/greedycomparisons  // greedycomparisons is the file where I'm creating the dir from
.../projct/data                    // the directory I want to create, to have my text files in

More specifically:进一步来说:

I'm trying to create the new subdirectory from a file I have inside my src folder.我正在尝试从我的 src 文件夹中的文件创建新的子目录。 Therefore, the thing I tried first was:因此,我首先尝试的是:

File directory = new File("./data/");
if (!directory.mkdir()) dosth;    // nothing happens on first call
/* making the String I want as my file's name, say string */
File file = new File(directory, string);

Which works fine (on Intellij), creating the subdirectory (and the files later on) exactly where I want them to.哪个工作正常(在 Intellij 上),准确地在我想要的位置创建子目录(以及稍后的文件)。 And then, likewise, I try accessing the files I made there from another file in my src folder, by again using "./data" before the name of the file I was trying to access, which again works as expected on Intellij.然后,同样地,我尝试访问我从 src 文件夹中的另一个文件创建的文件,方法是再次在我尝试访问的文件名前使用“./data”,这在 Intellij 上再次按预期工作。

But when I tested this on cmd, I need to change the directory name to "../data" in order for the code to work, which in turn does not work on Intellij (specifically giving me a "The system could not find the path specified" error) when I change it to that.但是当我在 cmd 上测试它时,我需要将目录名称更改为“../data”以使代码正常工作,这反过来在 Intellij 上不起作用(特别是给我一个“系统找不到路径指定”错误)当我将其更改为该路径时。 Given that I have to submit it as an assignment and I don't want any ambiguity in regards to my files, is there something more "universal" that I can try, so my code works regardless?鉴于我必须将其作为作业提交并且我不希望我的文件有任何歧义,是否有我可以尝试的更“通用”的东西,所以我的代码无论如何都能工作?

If you check your IntelliJ configuration, by clicking at the top where you have the name of the file you are executing, you can click on Edit Configurations, and you will see one of the fields you can configure, called "Working directory".如果您检查您的 IntelliJ 配置,通过单击顶部有您正在执行的文件的名称,您可以单击编辑配置,您将看到您可以配置的字段之一,称为“工作目录”。

IntelliJ is smart enough to set it to your project folder, outside the /src folder, because it is not good practice to write data inside your /src folder which should only have source files, unless they are resources you want to commit with your code. IntelliJ 足够聪明,可以将它设置到你的项目文件夹,在/src文件夹之外,因为在你的/src文件夹中写入数据不是一个好习惯,它应该只有源文件,除非它们是你想用你的代码提交的资源.

On the other hand, when executing it from cmd , your working directory is just where you are trying to run the compiled program from.另一方面,当从cmd执行它时,您的工作目录正是您试图从中运行已编译程序的地方。 You haven't specified in which folder you are in when doing so, but most probably you are inside some subfolder that has the classes, which is why you needed to do the .. .这样做时您没有指定您所在的文件夹,但很可能您位于某个包含类的子文件夹中,这就是您需要执行..的原因。

If you package your classes in a .jar file, the way IntelliJ made it should work.如果你 package 你的类在.jar文件中,IntelliJ 的方式应该可以工作。 If you run your .jar file with java -jar myjarfile.jar and the ./data folder is at the same level, then it would work.如果您使用java -jar myjarfile.jar运行.jar文件并且./data文件夹处于同一级别,那么它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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