While there are some techniques that allows you to create the perfect (and not so perfect) batch file hybrids with some 'native' windows script languages.
What a 'perfect' hybrid should look like:
- The embedded code must be usable as it is and you should be capable to copy-pasted it in any other editor/IDE you want. There should be no endless echoes to temp files and weird escape sequences.(rather possible for every language that support multi-line comments).
- No 'poisonous' prints of error messages (e.g. /* will print an error in command prompt despite the execution of the batch will continue on the next line)
- No temporary files.With the exception of compiled binary files which is unavoidable for the languages that does not have an interpreter.
Is it possible to create the 'perfect' java/batch hybrid?
Didn't you saw my DosTips post on Alternate Data Streams? This method have all the advantages you want for perfect hybrid scripts because the non-Batch code is stored in its own space (alternate stream), so it don't requires a single additional character! The code can be edited with Windows Notepad. The "only" problem is if the java compiler was designed to recognize the Alternate Data Stream. I successully tested this method with VBS, JScript and PowerShell, but unfortunately I have not a java compiler installed...
However, the test to check if this method work with your java compiler is very simple. Copy the code below and create BatchJavaTest.bat file with it:
Now we need to create the ADS with the java code. To do that, enter the following in the command-line:
When Notepad open, paste the following on it:
Save this file and test the Batch one. That is it!
Please, report the result!
PS - This program must be tested in a NTFS disk in order for it to work...
The answer is almost.
The main impediment is that the all compilers I know are very strict about the file extensions and will refuse to compile any file that has no
.java
extension.As any command in batch files starting with
@
will be not displayed we can use the java annotations to silence the error message that comes from the java comment (should be saved with.bat
extension):The example above covers points 1. and 2. but of course there's still need of creation of .java file.Still copying is faster than echoing java content line by line.
One step further (or half step) - making a
.java
extension to act like.bat
(or almost)Lets say you don't the like the part in the code that finds the java class name and the self-copy code. You can make the .java extension to act like .bat file.Or almost - the
%0
will be lost and the file name will be stored in%1
. For that purpose you need to call this batch file with admin permissions:Then the self-compiled .java file will look like this (should be saved with
.java
extension):there still will have a creation of temp
.bat
file by thejavaCaller.bat
but it will be not 'visible' and batch part is much shorter.In the examples there are no packages as the only single java file is used .Packages will make only the example harder to understand.