Hi,
I have recently discovered NetRexx, and it has rekindled my interest in Java again. However, my experience with the Windows batch file which invokes NetRexx was not as great.
Therefore, I humbly submit for your consideration (and for possible adoption in the NetRexx distribution), a new Windows invoker script. Apologies for the lengthy post. The main advantages of this script are: 1) This script seamlessly handles as many command line options you can throw at it, subject to Windows command line length limits. No more 9 parameter limits!
2) Another significant advantage is that this script attempts to overcome Windows command line quoting limitations. If you use the current Classic Rexx invoker script, your quoted arguments may be sent from Rexx to Java with the original quotes missing. This may or may not cause a problem for your NetRexx program.
2) This script uses WSH (Windows Script Host), which is bundled with every version of Windows since Windows 98. Handy when running NetRexx on environments where you don't have Classic Rexx or ooRexx available.
3) This script does extensive checking on the Java environment, and whether the file being invoked exists. In addition, it also checks the file name to ensure that it is a valid Java class name. File names which are not valid Java class names will cause the compiler to barf. As a NetRexx newbie, I encountered this countless times, so this script will prevent (most) of those errors from happening in the first place.
I am uncertain on the best method to attach the two files which make up the new script, so I am embedding them in the body of this message. Please copy from and paste into a text file, with the names mentioned in the enclosed blocks. Don't copy the block delimiters containing the asterisk (*) characters, though.
If this doesn't work, please advise on the best attachment method, and I shall repost. You should end up with two files in the same folder: netrexx.bat and netrexx1.vbs. You should also edit the paths to your NetRexx jar files, and your Java installation in the 'CONFIGURATION SECTION' inside netrexx1.vbs. NOTE: You MUST edit the paths manually or the script will fail.
Once you have done the above, run: netrexx.bat (to get usage options) OR netrexx.bat filename.nrx [NetRexx options, if any] (to compile to a .class file)
OR netrexx.bat filename.class [NetRexx options, if any] (to run the compiled .class file) The netrexx1.vbs script is quite well documented, so you should go there for more information first.
Current limitations: 1) Compiles one file at a time. If you frequently compile a lot of files in one go, put the build into a batch file and invoke netrexx.bat on each file.
2) The current regex used to check for file names will allow filenames made up of all underscores as a valid name. This is incorrect as Java classes need underscores and other characters. Windows filenames, however, can be made up of underscores only!
3) Those to run graphical programs will see the black command window pop-up first. This is not be desirable for graphical programs, and may be fixed in a later release. 4) The script only runs on Windows. Sorry. Your feedback is welcome. Regards, Ramesh Gopal
********** BEGIN FILENAME: netrexx.bat ********** @echo off rem rem The reason for the convoluted invocation of netrexx1.vbs is because VBScript
rem does not provide access to the raw command line. So our trick is to pass it rem first to this batch file, which will then store the raw command line in an rem environment variable. Later, we invoke netrexx1.vbs and it will be able to
rem access the raw command line from the environment variable. rem rem This script depends on cscript.exe, which should usually be on your PATH. If rem it isn't, track it down and put the directory where it resides on your PATH.
rem set NRIGNORE= set NRCMDLINE= for /f "usebackq tokens=1*" %%i in ('%*') do ( set NRIGNORE=%%i set NRCMDLINE=%%j
) cscript.exe //Nologo "%~dpn01.vbs" %* ********** END FILENAME: netrexx.bat ********** ********** BEGIN FILENAME: netrexx1.vbs **********
' NETREXX1.VBS ' NetRexx Invoker Script Wrapper for Windows ' ' Author(s): Ramesh Gopal, 2013 ' ' You are free to use, distribute or modify this script, so long as you bear
' all responsibility for using this script, and indemnify the author(s) from ' any and all claims arising from the use of this script. ' ' For usage, run netrexx.bat without any parameters.
' ' This script makes it easier to compile and run NetRexx scripts. This script ' has a number of advantages over the batch file script that ships with the ' NetRexx distribution. In no particular order:
' ' 1) This script can supply as many command line options as you care to ' provide, limited only by Windows' command line limits. This overcomes the ' 9 parameter limit that the existing batch file has.
' ' 2) This script checks if the file specified exists before invoking the ' NetRexx compiler or runtime. It also checks for correct file extensions, ' and most importantly, it also ensures that the filenames chosen for
' NetRexx scripts conform to Java's identifier requirements. This means ' this script will correctly reject NetRexx script file names which contain ' invalid characters such as spaces, hyphens, etc. Even filenames which
' begin with numbers will not compile correctly, and this script will abort. ' This feature relies on WSH's built-in regular expressions support, which ' is not built into, and is difficult to emulate in, Classic Rexx.
' ' 3) This script compiles and runs scripts separately. That means you first ' need to invoke the script to compile your NetRexx file, and then run it ' again to execute the resulting .class file. You could also choose to
' just interpret the script using the -exec or -arg options. ' ' 4) This script runs using WSH and VBScript, which ships with every version ' of Windows since Windows 98. This means you don't need a Classic Rexx
' interpreter to break the 9-parameter limit of the existing batch file. ' So you can run NetRexx easily on (almost) any Windows machine even if ' you don't happen to have a Classic Rexx interpreter handy. Caveat: I
' tested this script only under Windows 7 Ultimate, so if you encounter ' any problems due to your environment, post a note on the NetRexx forum, ' and I will try to assist (no guarantees, though).
' ' 5) Proper command line quote handling. Windows command line quoting is ' confusing and annoying. This script keeps your quotes intact until it ' reaches Java, thus perhaps sparing you the unpredictable results that
' can result from passing quoted command line arguments from one script to ' another. This problem may occur when moving quoted command-line options ' from the Classic Rexx NetRexx invoker script to Java.
' ' 6) This script demonstrates how to perform some tasks under VBScript. If ' you're learning VBScript, this script demonstrates quite a few tricks, ' including how to launch and redirect output from a command, and a few
' others. ' ' The only drawback this script has right now is there is a slight performance ' impact on console output. So don't use this script to run a .class file if
' performance is paramount, or if benchmarking is being done with console- ' heavy programs. Scripts that make little or no use of the console should not ' be affected by this performance impact.
' ' ' USAGE NOTES ' ' The file name must come right after the script name. The script checks the ' file name for several conditions:
' 1) That the file exists. ' 2) That the file extension is either .nrx or .class. ' 3) That the file name conforms to Java naming conventions. ' ' This script also checks that all the NetRexx prerequisites exist before
' running. This includes java.exe, tools.jar and also the NetRexx jar files. ' You can and should change the settings in the 'CONFIGURATION SECTION' below ' to match your installation and preferences.
' ' This script does not allow compiling and running in a single step because ' of the possible number of options that could be sent to the compiler, and ' also to the compiled program. This would complicate options processing.
' However, if you desire this behaviour, feel free to make the changes ' yourself and share them with the community. ' ' You also need to ensure that netrexx.bat and netrexx1.vbs (this file) are
' in the same directory, and that directory should (ideally) be on your PATH. ' ' For now, this script is only for Windows, unless some script guru can ' translate it into bash, or its equivalent on other platforms.
' ' NOTE: This file is deliberately named netrexx1.vbs in order to avoid it ' being accidentally invoked if the extension is not specified. Do not ' rename it to netrexx.vbs unless you know the consequences of your actions!
' And while you're at it, don't rename netrexx.bat as well for the same reasons. ' ' ' TODO ' ' 1) Need a better regex to detect valid Windows file names which are solely
' made up of underscore characters, but these are invalid Java identifier ' names. ' 2) Maybe create another version of the invoker for graphical programs. This ' script will currently create a command window first, and then invoke Java
' and NetRexx. This behaviour may not be desirable for graphical programs. Option Explicit Dim ShowDebug Dim java_path, netrexx_path, tools_path, java, netrexxc, netrexxr, tools
' BEGIN CONFIGURATION SECTION ' Set ShowDebug to True if you want Debug statements to take effect. ShowDebug = False ' Change the paths below to match your installation.
' These paths MUST be correct for NetRexx to function properly. ' The path to tools.jar is not needed if you're only interpreting NetRexx programs. java_path = "C:\Windows\system32\"
netrexx_path = "C:\downloads\rexx\netrexx\bin-302\" tools_path = "c:\Program Files\Java\jdk1.7.0_21\lib\" ' The following files MUST exist for NetRexx to function properly.
java = java_path & "java.exe" netrexxc = netrexx_path & "NetRexxC.jar" ' The file tools.jar is not needed if you're only interpreting NetRexx programs.
tools = tools_path & "tools.jar" ' The file NetRexxR.jar is not mandatory as the same functionality is also ' inside NetRexxC.jar. netrexxr = netrexx_path & "NetRexxR.jar"
' END CONFIGURATION SECTION 'SUB Say 'DESCRIPTION ' A convenience function to print a message to the console. ' This function could be named anything we wanted, but I thought
' Say should be an old friend to Rexx programmers. 'IN ' s: String to print to the console. 'RETURNS ' None Sub Say(s) WScript.Echo s
End Sub 'SUB Debug 'DESCRIPTION ' A convenience function to print a message to the console. ' Toggling the ShowDebug variable will turn on / off debug output.
'IN ' s: Debug string to print to the console. 'RETURNS ' None Sub Debug(s) If ShowDebug Then
WScript.Echo s End If End Sub 'FUNCTION ValidClassName
'DESCRIPTION ' This function checks if the input string contains characters ' which violate Java rules for identifiers. The regex pattern ' used here is not foolproof nor industrial-strength! For example,
' the current regex allows one or more underscores as a valid name. 'IN ' s: Input string to compare against regex pattern. 'RETURNS ' True if the input string contains invalid characters,
' False otherwise. Function ValidClassName(s) Dim matchlen, regEx, Match, Matches matchlen = 0
Set regEx = New RegExp regEx.Pattern = "[_A-Za-z]+[_A-Za-z0-9]*" regEx.IgnoreCase = False
regEx.Global = False Set Matches = regEx.Execute(s) For Each Match in Matches
matchlen = Match.Length Next If matchlen = Len(s) Then
ValidClassName = True Else ValidClassName = False
End If End Function 'FUNCTION RunCommand 'DESCRIPTION ' This function runs a command, displays its output on the console,
' and returns its exit code. 'IN ' s: Input string of command to run. ' ShowOutput: Boolean indicating whether or not to display console output. 'RETURNS
' Exit code of command which was run. Function RunCommand(s, ShowOutput) Dim oExec, out, err Set oExec = wsh.Exec(s)
Set out = oExec.StdOut Set err = oExec.StdErr Do While oExec.Status = 0
If ShowOutput Then If Not out.AtEndOfStream Then WScript.StdOut.Write out.ReadAll()
End If If Not err.AtEndOfStream Then WScript.StdErr.Write err.ReadAll()
End If End If WScript.Sleep 50 Loop
If ShowOutput Then If Not out.AtEndOfStream Then WScript.StdOut.Write out.ReadAll()
End If If Not err.AtEndOfStream Then WScript.StdErr.Write err.ReadAll()
End If End If RunCommand = oExec.ExitCode End Function
Sub DisplayUsage Say "" Say "NetRexx Invoker Script Wrapper for Windows"
Say "" Say "Usage: netrexx.bat <NetRexx file>.<.nrx|.class> [args ...]"
Say "" Say "If a .nrx file is specified, it is compiled to a .class file." Say "If a .class file is specified, it is run."
Say "" Say "The file extension (either .nrx or .class) is mandatory, as the script uses"
Say "it to determine whether to compile or run the script." Say "" End Sub Dim wsh, fso, env, f1, f2, args, basename, parentfolder, filename, extension, cmdstr, rawcmdline, rc Set wsh = CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject")
Set env = wsh.Environment("PROCESS") Set args = WScript.Arguments If Not fso.FileExists(java) Then Say "Cannot find java.exe."
WScript.Quit(1) ElseIf Not fso.FileExists(netrexxc) Then Say "Cannot fine NetRexxC.jar."
WScript.Quit(1) ElseIf args.Count < 1 Then DisplayUsage Say "No file name provided. Displaying NetRexx options ..."
Say "" Say "" cmdstr = java_path & "java.exe -jar " & netrexx_path & "NetRexxC.jar"
rc = RunCommand(cmdstr, True) WScript.Quit(1) End If rawcmdline = env("NRCMDLINE")
basename = "" parentfolder = "" filename = "" extension = "" If Not fso.FileExists(args(0)) Then Say "File " & args(0) & " does not exist."
WScript.Quit(1) End If basename = fso.GetBaseName(args(0)) Set f1 = fso.GetFile(args(0)) parentfolder = f1.ParentFolder
filename = f1.Name If Not ValidClassName(basename) Then Say filename & " is not a valid NetRexx file name. Valid NetRexx files must be valid Java identifiers."
WScript.Quit(1) End If If Right(filename, 6) = ".class" Then extension = ".class"
ElseIf Right(filename, 4) = ".nrx" Then extension = ".nrx" Else Say filename & " does not have a valid NetRexx file extension. Valid file extensions are either .nrx or .class"
WScript.Quit(1) End If If extension = ".nrx" Then If Not fso.FileExists(tools) Then
Say tools & " does not exist. Please install tools.jar first." WScript.Quit(1) End If
If Not fso.FileExists(netrexxc) Then Say "Cannot find file " & netrexxc & "."
WScript.Quit(1) End If cmdstr = java & " -cp " & _
Chr(34) & tools & Chr(34) & ";" & _ Chr(34) & netrexxc & Chr(34) & ";" & _
Chr(34) & parentfolder & Chr(34) & " " & _ "org.netrexx.process.NetRexxC" & " " & _
basename & " " & rawcmdline Else If Not fso.FileExists(netrexxr) Then 'Say netrexxr & " does not exist. Using NetRexxC.jar"
netrexxr = netrexxc End If cmdstr = java & " -cp " & _
Chr(34) & netrexxr & Chr(34) & ";" & _ Chr(34) & parentfolder & Chr(34) & " " & _
basename & " " & rawcmdline End If ' If you encounter issues with compiling or running programs, it may be
' useful to set ShowDebug to True and view the command that is generated ' by this wrapper script, and also the return code from Java after running ' NetRexxC.jar or NetRexxR.jar.
Debug cmdstr rc = RunCommand(cmdstr, True) Debug "rc: " & rc ********** END FILENAME: netrexx1.vbs ********** _______________________________________________ Ibm-netrexx mailing list [hidden email] Online Archive : http://ibm-netrexx.215625.n3.nabble.com/ |
Welcome Ramesh Gopal! This shows a high degree of fit and finish, and I'm looking forward to trying it out. Just one question for now: won't the limitation of compiling one class at a time prevent compiling a pair of classes that refer to one another? On Fri, Jun 7, 2013 at 12:15 PM, Ramesh Gopal <[hidden email]> wrote:
-- "One can live magnificently in this world if one knows how to work and how to love." -- Leo Tolstoy _______________________________________________ Ibm-netrexx mailing list [hidden email] Online Archive : http://ibm-netrexx.215625.n3.nabble.com/ |
Hi George,
You raise a good point. My initial goal for this script was to make it easy and a little fool-proof for NetRexx newcomers. When learning NetRexx, I was only experimenting with single-class files. I believe this script makes it very easy for someone using the Windows command line to get NetRexx up and running quickly. However, your point is valid, and I shall mark it down on the list of fixes and enhancements. I would also like to hear from other forum members on other fixes or enhancements that would make the script better all round. Thanks for taking the time to comment. Ramesh On Sat, Jun 8, 2013 at 2:19 AM, George Hovey <[hidden email]> wrote:
_______________________________________________ Ibm-netrexx mailing list [hidden email] Online Archive : http://ibm-netrexx.215625.n3.nabble.com/ |
Free forum by Nabble | Edit this page |