Library code snippets

Compiling and executing Java Code in Visual Basic

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

  1. 25 Jan 2007 at 12:14

    Hi,

    Can you give the same type of example in C#.

    As i ahve a requirement that i need to compile the java code which is wriiten in tetxarea and need to display the errors if any.

    i am doing a C# windows application in which there is one richtextbox in which user need to enter the java code and when the user clicked the compile button the code written need to be compiled and shows the error messages if any.

    Can you please help me in this regard.

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Compiling and executing Java Code in Visual Basic.

Leave a comment

Sign in or Join us (it's free).

 Obaid

Related discussion

Related podcasts

  • Java Posse #213 - Newscast for Oct 23rd 2008

    Newscast for Oct 23rd 2008 Fully formatted shownotes can always be found at http://javaposse.com The Android project has been released as open source, beating the rumored launch date for the source code by several months http://source.android.com/ And, Gizmodo and ZDNet both offer in-depth ...

Events coming up

  • May 19

    Google I/O 2010

    San Francisco, United States

    Google's largest developer event returns to San Francisco in 2010. Google I/O brings together thousands of developers for two days of highly technical content, focused on pushing the boundaries of web applications through open web technologies and Google developer products like App Engine, Google Web Toolkit, Android, Chrome, APIs, and more. Early registration for Google I/O will open in January 2010.

Want to stay in touch with what's going on? Follow us on twitter!