Introduction
If you are familiar with Java programming then you would probably know that one has to code in some text editor and then use Command Prompt to compile and run them by using the same old commands. Now that is indeed very annoying and of course there are many editor which allow you to compile and show the output of your code right there and then. Here I am going to show you how you can do this in Visual Basic. The program will ask for JDK Path and the Path of your code.
Compiling your code:
Before we continue make your self aware of the following Dos commands:
Echo
Turns the command-echoing feature on or off, or displays a message. Used without parameters, echo displays the current echo setting. If you want to turn echo off and you do not want to echo the echo command, type an at sign (@) before the command as follows: @echo off
Pause
Suspends processing of a batch program and displays a message prompting the user to press any key to continue. the following message appears:
Press any key to continue . . .
We will create a .bat file write to it what we do manually i.e
@ECHO OFF
echo Compiling. . . C:\article\Sample.java
c:\jdk1.3\bin\javac.exe -verbose C:\article\Sample.java
echo Done
pause
Now this is the code for compiling your code with *.java extention
Private Sub Command1_Click()
Dim compile As String 'location of .bat file
Dim jdk As String 'location of jdk
Dim fileno As Integer
compile = App.Path & "\compile.bat"
jdk = "c:\jdk1.3" 'You may need change this
On Error Resume Next
Kill compile 'Kill the compile.bat if it exist
fileno = FreeFile
Open compile For Append As fileno
Print #fileno, "@ECHO OFF" 'Dos command to hide command prompt
Print #fileno, "echo Compiling. . . " & CommonDialog1.FileName
Print #fileno, jdk & "\bin\javac.exe -verbose " & CommonDialog1.FileName
Print #fileno, "echo Done"
Print #fileno, "pause"
Close fileno
Shell compile, vbNormalFocus
End Sub
Running you code
Code for running the code is:
Private Sub Command2_Click()
Dim run, jdk, temp, file1 As String
run = App.Path & "\run.bat"
On Error Resume Next
Kill run
jdk = "c:\jdk1.3"
file1 = Left(CommonDialog1.FileTitle, Len(CommonDialog1.FileTitle)
- 5)
Dim fileno As Integer
fileno = FreeFile
Open run For Append As fileno
Print #fileno, "@ECHO OFF"
Print #fileno, "echo Output of: " & file1
Print #fileno, "java.exe " & file1
Print #fileno, "pause"
Close fileno
Dim rc As Double
rc = Shell(run, vbNormalFocus)
End Sub
And don’t forget to add the following to open a *.java file: (you will need to add a Common Dialog control called CommonDialog1 to your form)
Private Sub Command3_Click()
Me.CommonDialog1.Filter = "Java source code
Files (*.java)|*.java|All Files (*.*)|*.* "
Me.CommonDialog1.ShowOpen
Text2 = Me.CommonDialog1.FileName
If CommonDialog1.FileName = "" Then
Exit Sub
End Sub
Now you can compile and view the output of your program.
Comments