Why are my Java code changes not being reflected when ran from command line but working in Eclipse?

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

Why are my Java code changes not being reflected when ran from command line but working in Eclipse?

问题

我正在使用一个 bash 脚本来运行我在 Eclipse 中制作的 Java 程序,当我从 Eclipse 运行 Java 程序时,它能够正常运行。通过一些我刚刚插入并再次运行的打印语句,我可以确认它包含了我最近的更改。

然而,当我运行我的 bash 脚本时,这些打印语句和我所有的其他更改都无法被识别出来。这个 bash 脚本只是像这样运行程序(使用 testNG):

java -cp ".\src\main\java;lib\*" org.testng.TestNG ParallelTestXML.xml

我已经在 Eclipse 中清理了项目,并确保已经点击了自动构建,尽管我认为这是用来修复 Eclipse 中未编译最近更改的问题的。所以我真不知道还可能是什么原因。

英文:

I am using a bash script to run my Java program that I made in Eclipse and the Java program is working fine when ran from Eclipse. It has my most recent changes which I can tell by some print statements that I just inserted and ran again.

However, these print statements and all my other changes are not being seen when I run my bash script, which literally just runs the program like this (using testNG):

java -cp ".\src\main\java;lib\*;" org.testng.TestNG ParallelTestXML.xml

I have already cleaned the project in Eclipse and made sure build automatically is clicked, although I think that is to fix if it isn't compiling recent changes within Eclipse. So I have no idea what else it could be.

答案1

得分: 1

因为 .\src\main\java 没有做任何有用的事情。

Eclipse 有一个叫做 'builders' 和 'project kinds' 的概念,根据你如何设置你的 Java 项目,Eclipse 的构建-保存架构工作方式不同。

假设你只是按照以下步骤进行操作: "File > New Project > Java Project",并选择了所有默认选项,那么 Eclipse 的设置方式是你有一个 src 目录(事实上,你写 src/main/java 暗示你没有这样做,但为了举例,我会继续),当你在 Eclipse 中保存任何 Java 文件时,Eclipse 会立即更新这个文件的一个构建视图,它将位于项目根目录下的一个名为 bin 的目录中。

那里是类文件的所在地,所以如果你想在命令行上运行它们,正确的操作是:

java -cp ".\bin;lib\*;" org.testng.TestNG ParallelTestXML.xml

添加 src 目录是完全没有意义的,除非类文件就在源文件旁边,否则将该目录命名为 src 显然非常愚蠢(通常在编程中,选择一个明显虚假的名称是一个非常糟糕的主意,理由显而易见)。

如果你有其他项目设置,例如,你将其设置为 Maven 项目或 Gradle 项目,那么它取决于你如何配置 Eclipse,Eclipse 是否试图'匹配'构建,或者是否在每次保存时触发一个完整的 Maven 构建,或者是否应该手动调用 Maven。最有可能是后者。让 Maven 来进行构建,然后 Maven 将在某个地方构建你的东西。通常是 {projroot}\target\classes,但如果你的应用是使用 Maven 构建的,那么在运行应用时,不要调用 java,而是调用 mvn,要求它测试你的东西。这样,Maven 将负责处理你的依赖关系等等。

英文:

Because .\src\main\java doesn't do anything useful.

Eclipse has this concept called 'builders' and 'project kinds', and depending on how you've set up your java project, eclipse's build-on-save architecture works differently.

Assuming you just went: "File > New Project > Java Project", and picked all the default options, the way eclipse is set up is that you have a src dir (the fact that you write src/main/java belies that you didn't do this, but I'll continue for the sake of example), and when you save any java file in eclipse, eclipse will immediately update a built view of this, and it will be in a dir hanging off of the project root called bin.

That's where the class files live, so if you want to run off of those on the command line, the right move is:

java -cp ".\bin;lib\*;" org.testng.TestNG ParallelTestXML.xml

Adding the src dir is completely pointless, unless the class files live right next to the source files, in which case calling that dir src is obviously very silly (as a general rule in programming, picking a name that clearly lies, is a very bad idea, for obvious reasons).

If you have some other project setup, for example, you've set it up as a maven project or a gradle project, well, it depends on how you configured eclipse whether eclipse is trying to 'match' the builds, or is triggering a full maven build every time you save, or if you're supposed to invoke maven manually. Most likely the latter. Let maven do the building, and maven will then build your stuff someplace. Generally, {projroot}\target\classes, but to 'run' your app if your app is built with maven, don't invoke java. invoke mvn, asking it to test your stuff. That way mvn will take care of your deps and the like.

huangapple
  • 本文由 发表于 2020年8月29日 21:43:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63647626.html
匿名

发表评论

匿名网友

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

确定