简体   繁体   中英

Why are my Java code changes not being reflected when ran from command line but working in 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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