0

I have a batch script that uses the Call function, but I'm noticing different behavior when it's ran on my Windows 7 machine vs my Windows 10 machine. I wasn't able to find much on this issue specifically for differences between Windows versions.

The script is simply:

@echo off
chdir /D "%~dp0"
call "C:\Users\username\desktop\temp\batch.bat" >Test.txt
pause

and within the batch.bat, there is a single line of java:

@echo off
cd /D "%~dp0"

java -classpath "../jars/*" com.scraper_program.cui.Main show --log="../conf/log.settings"

On Windows 7, this correctly outputs the text from the call to java into the file Test.txt; however on Windows 10, it opens the java portion in a new window and fails to re-direct to the text file.

The new window opened has a 'java' icon in the upper left corner, so it looks like Windows10 may be treating the Java portion differently for some reason.

Everything about the location of the script and variables are identical between the two machines. Is there anyway to make the script output to the text file in windows 10 like it does in windows 7?

Edit:

The problem is with the java console. This being a batch file and the code don't matter so all of the above is basically worthless information :(

The issue is reproducible by simply typing in "java -help" into the command line. On the windows 7 machine, it outputs the help text directly to the console while on my windows 10 machine it opens a new window, executes, and closes immediately. I found another post with the same issue here: Java execution pops a new window and immediately disappears

However there wasn't a resolution to it.

Drag and dropping the java.exe file directly into the console in order to provide the full path to the exe gives the same result - it flashes on screen briefly and closes.

I've also verified that the Java settings in control panel are identical between the two systems and that the same environment paths to Java.exe are the same.

Edit 2:

By request, here are my environment variables.

Output from set Path:

Path=C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Microsoft DNX\Dnvm\;C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\;C:\Program Files\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Gpg4win\..\GnuPG\bin;C:\Drivers
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

Output from where Java:

C:\ProgramData\Oracle\Java\javapath\java.exe
Bob Jordan
  • 23
  • 1
  • 4
  • 2
    Theres absolutely no reason why `Windows 10` would behave any different from `Windows 7`. You've provided a batch script containing an undefined variable, `%DIR%`, and one containing an incomplete line. Because `%DIR%` is not a standard environment variable, and you've not indicated that it has been pre-defined, your code will be `call`ing `\batch.bat`. **`\ `** means the root directory, so in essence it is trying to run `batch.bat` in the root directory of the drive relative to the current working directory, probably `C:\ `! – Compo Jul 19 '18 at 22:39
  • Hey @compo - thanks for the reply The batch is actually a lot longer, but I just included the relevant line. Dir is an obfuscated variable name and defined correctly further up the script. Similarly with Java, I just included the first bit to show what it looks like. Sorry, probably should have clarified this in the original post - I'll edit it. Still, if there's no reason windows 10 should behave differently, then why am I getting different results? As mentioned, it works properly on windows 7. – Bob Jordan Jul 19 '18 at 23:40
  • Hi @Mofi - I've added the results of set path and where java under edit 2. I do see the directory for java within the path and the problem isn't that it's not executing the right command, but rather that its executing in a new window. Running java -help does display the help dialog, but just does so super briefly and in a new window that closes immediately after the help text is output. Do you happen to see anything odd in the paths listed? – Bob Jordan Jul 24 '18 at 03:24
  • `PATHEXT` is fine. `PATH` is not good defined. There should be no folder path before the four standard Windows folder paths starting with `%SystemRoot%`, i.e. C:\Windows. Edit your __system__ `PATH` via __Control Panel - System - Advanced system settings - Environment variables__ and move the first three folder paths downwards or right after PowerShell path. Also folder paths in `PATH` *can* but __should not__ end with a backslash. The trailing backslashes should be removed from the folder paths, see https://stackoverflow.com/questions/50989592/ – Mofi Jul 24 '18 at 04:59
  • The last but one folder path should be also replaced by real absolute path `%ProgramFiles(x86)%\GnuPG\bin` and `C:\Drivers` should be really removed from `PATH`. But none of these minor `PATH` issues are responsible for running Java always in a new console window using `cmd /C`. [Oracle Java on Windows: C:\ProgramData\Oracle\Java\javapath](https://marxsoftware.blogspot.com/2015/07/windows-oracle-java-path.html) explains that the folder `C:\ProgramData\Oracle\Java\javapath` just contains symbolic links to Java's real executables. – Mofi Jul 24 '18 at 05:08
  • What happens on replacing in __system__ `PATH` this folder path with the symbolic links by real `bin` folder path of installed Java? Which version of Java do you have installed at all? Do you have verified that there is no `java.bat` or `java.cmd` in the directory of your batch file? What happens in using in your `batch.bat` file as third line `for %%I in ("%~dp0..\") do set "BasePath=%%~fI"` and use in `java` command line twice `%BasePath%` instead of `../` and of course also ``\`` instead of `/` as the backslash is the directory separator on Windows and not the forward slash? – Mofi Jul 24 '18 at 05:17

1 Answers1

0

I found a way to get the call from the java line to output into the same window and properly re-direct to the text file by running the command line as an administrator (right click -> run as administrator).

Once I found that out, I followed the solution posted here to make a shortcut for the bat file: How to code a BAT file to always run as admin mode?

I'm still not sure why I need to have this work around in place on one machine and not the other so it's not perfect, but it does work.

Bob Jordan
  • 23
  • 1
  • 4