1

I run my java source code through a batch file. The problem is, when I make any change in my source code through any text editor like notepad etc. and save the code, the changes do not reflect back when I run the code through my batch files.

Below is the sample of my batch file that I use to run my code.

cd %cd%
set classpath=%cd%\target\classes;%cd%\lib\*
echo %classpath%
echo %cd%
java org.testng.TestNG %cd%\code.xml
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"  %cd%\test-output\report.html
pause
Amadan
  • 191,408
  • 23
  • 240
  • 301
Arpit Agarwal
  • 161
  • 1
  • 1
  • 6
  • Your script doesn't recompile the sources (`javac`), so it's no surprise the changes aren't reflected. Remember: javac to turn `.java` -> `.class`, and then java to run the `.class`. – yshavit Jul 23 '18 at 03:43
  • can I do it in my batch file directly or I have to do that it in cmd separately? – Arpit Agarwal Jul 23 '18 at 03:46
  • no reason you can't do it in the script. – yshavit Jul 23 '18 at 03:47
  • Doing the compilation in the script would run it each time you start the program. However, it is only necessary when the program has changed. This may not matter as long as your program is small, but you definitely don't want it in real world cases. – Henry Jul 23 '18 at 03:50
  • OK, I will surely do as before. – Arpit Agarwal Jul 23 '18 at 03:50
  • still if there is a way to recompile the code through script, please help me out. – Arpit Agarwal Jul 23 '18 at 03:52
  • @ArpitAgarwal You got a new answer, it could provide the wanted help – Mischa Jul 23 '18 at 04:10

3 Answers3

1

Disclaimer: This was started as an edit for paulsm4's answer. But in my opinion this change got too big to be a justified edit.

Changes you make to source files will never take effect until you recompile your code.

You can do this by:

  1. Calling javac (or an equivalent java compiler) manually or within your batch file.

    Usage of javac based on this documentation:
    javac [ options ] [ sourcefiles ] [ classes ] [ @argfiles ]

    Arguments may be in any order.

    options
    Command-line options.

    sourcefiles
    One or more source files to be compiled (such as MyClass.java).

    classes
    One or more classes to be processed for annotations (such as MyPackage.MyClass).

    @argfiles
    One or more files that lists options and source files. The -J options are not allowed in these files.

  2. Have an IDE (like eclipse) do it for you

    The IDE will build the application on run if the code has changed

  3. Use a build tool like:

A build tool combined with an IDE is what most developers do. A build tool is also used for dependency management. Which makes it great if more than one person works on a project, or when the project is moved between machines often.

Mischa
  • 1,303
  • 12
  • 24
0

Of COURSE the "changes you make in notepad" won't take effect ... until you RECOMPILE YOUR code.

You need to execute the "javac" compiler (or equivalent) before the next time you run "java".

Which is precisely why C/C++ programmers use "make", and Java programs have used "Ant".

STRONG SUGGESTIONS:

  1. Get an IDE (I prefer Eclipse, but there are other, excellent choices):

    https://www.eclipse.org/downloads/

  2. Learn a build tool (Ant is still very useful):

    http://www.mkyong.com/tutorials/apache-ant-tutorial/

A quick'n'dirty workaround is to simply add "javac" to your .bat file. But that means you recompile every single time you run the .bat file - whether you need to or not. That works OK when your program consists of one or two source files ... but it doesn't scale when your app grows to 10s or hundreds of source files.

Familiarize yourself with an IDE, and familiarize yourself with build tools. Time well spent :)

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks for your suggestion. I too prefer eclipse only, but I forgot the concept of compilation. Well, thanks again for the help. – Arpit Agarwal Jul 23 '18 at 03:44
0

Check, if the source code is newer than the compiled code (I have no idea about Java, so you surely have to adapt the extensions - feel free to adapt it in this answer too to help others, who might stumble over this)

for %%F in (program.txt) do set file=%%~fF
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set datetimeSource=%%I
for %%F in (program.exe) do set file=%%~fF
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:\=\\%" get lastmodified /format:list') do set datetimeCompiled=%%I

if "%datetimeSource%" gtr "%datetimeCompiled%" echo insert here your command to compile the code...

(See here for the reasons for that strange syntax)

wmic is quite slow, so this code snippet needs about a second, but it's the most reliable way to get a comparable format.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • I myself trying to analyse what the code does and where it should be implemented. – Arpit Agarwal Jul 23 '18 at 11:05
  • the first two lines get the "last modified" date/time of your source code file (adapt the filename.extension). the next two lines does the same for the compiled file (again, adapt the filename.extension). The `if` line checks, if the source file is newer than the compiled file (where `wmic` gives a comparable format of `YYYYMMDDhhmmss....`). As where to put it: how about immediately before executing the compiled file? – Stephan Jul 23 '18 at 13:45
  • For anything more complicated than a one-off/compile always scenario, a .bat file is a really, really bad idea. SUGGESTIONS: 1) Simplest case: add `javac MyClass.java` to your bat file. 2) "More than a toy project case": learn and use Ant, Maven or Gradle. – paulsm4 Jul 23 '18 at 20:24
  • [Bad ideas make the best memories](https://www.youtube.com/watch?v=lwf0dTW9oUI) `:D` – Stephan Jul 24 '18 at 09:38