More exploring: Vegas from java

RichMacDonald wrote on 2/20/2005, 5:15 PM
Continuing down the path of "shouldn't" and "can't", I've run into a sourceforge project called IKVM. Bottom line is that with a bit of bytecode trickery it allows interoperability between java and .net.

It actually works and not terribly painfully. I'm currently running an ikvm app in the Eclipse java IDE and am able to access a variety of .net functionalty. the whole mscorlib.dll is available to me and I can create objects, show MessageBoxes, etc. Good stuff and useful in its own right.

Connecting to Vegas is not so good. Right now I can't instantiate any vegas object from the Sony.Vegas.dll. Its a "class not found error". Not sure what the issue is yet. Could be limitations in ikvm or could be some fundamental setup error of mine. I've googled hints of both but am still looking.

I notice there isn't a way to instantiate the Vegas object in the Sony.Vegas.dll. I'm too ignorant to know what that means. Is it that Vegas is now not intended to be launched; instead a running vegas launches a JScript and the script passes the vegas instance to the code?

As an alternative, I tried to connect to a running instanceof vegas.

cli.System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

answers an instance of Microsoft Word. I've tried "Sony Vegas", "Sony.Vegas" "Vegas 5.0" ad nauseum and searched the registry for the ProgID of Vegas, but struck out. Can't get past a "COMException: Invalid class string" here.

Anyway, kindof exciting over here, but I suspect I know where this going to (not) work out.

Comments

rcampbel wrote on 2/20/2005, 8:45 PM
Rich,

You are correct that the script has to be passed an instance of Vegas. As you saw in the VegasHello example, the Vegas instance is passed from the JScript to the .Net dll.

I don't know of any other way to do it.

Randall
RichMacDonald wrote on 2/20/2005, 10:54 PM
I got it! I figured out how to write scripts in java. Don't know all the limitations but I got my java HelloWorld Vegas script working.

Its been a long day so here is a rough outline. I'm bound to have forgotten something so I'd really appreciate someone else trying to duplicate this and helping me clean up the instructions as we go. Still early days, so only follow along if you're a foolish early adopter ;-). I'll try and describe my system, but be aware that my development path was anything but linear so there is a definitely a ton of redundancy and unnecessary steps in the following. I assume you're a java-savvy developer who can grasp the concept and do it your own way. I'm just trying to document what I did this first time in I forget something when I get back to it ;-)

1) Download and install the latest release of the binary ikvm from sourceforge link to ikvm. You only need the binary. I installed to "c:\Progs\ikvm". its a simple zip extraction.

2) Ikvm is a java substitute. I develop in the Eclipse IDE so I want ikvm to look just like an installed java. See using ikvm with eclipse. These instructions are incomplete because they create a "jre" and eclipse needs a "jdk". So after you've created your bin and lib directories, copied "ikvm.exe" to "java.exe" in bin and created the rt.jar in lib, copy the "c:\Progs\ikvm" directory to "c:\Progs\ikvm\jre".

3) Now we need to create stub jar files for all the dlls that we'll use. This is the ikvmstub tool. I looked at all the references that Sony.Vegas.dll used, so I created jars for the interop.VegasCOM.dll, Microsoft.JScript.dll, Microsoft.VisualBasic.Vsa.dll, Microsoft.Vsa.dll, mscorlib.dll, Sony.Vegas.dll, System.Windows.Forms.dll, and System.Xml.dll. (This is doubtless overkill. For sure we need the mscorlib.dll and the Sony.Vegas.dll. I added all the others because I was getting an error and needed to eliminate a possible cause.) So run ikvmstub on each of these files and copy them to the "C:\Progs\ikvm\lib" and "C:\Progs\ikvm/jre/lib" directories. This too is probably overkill, i.e., we probably don't need them in both directories. Alternatively, you could say that I'm using poor code management techniques and should instead copy these files into each java project as needed and you'd be right.

4) My Windows PATH system variable includes:

C:\Progs\ikvm\bin;C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322

5) I created an Eclipse java project called VegasVideo, with src and bin directories. "C;\eclipse\workspace\VegasVideo". I configured the project to copy all the dlls in the ikvm/bin to the VegasVideo/bin drectory. The project uses the kvm java compiler rather than the default java compiler.

6) I created a java class called Main in the VegasVideo/src/Demo/VegasHello directory. The code is below:

package Demo.VegasHello;
import cli.Sony.Vegas.Vegas;
import cli.System.Windows.Forms.MessageBox;

public class Main {
public static void Start(Vegas v, String scriptFile) {
MessageBox.Show("Got here");
MessageBox.Show("Vegas version = "+v.get_Version());
}
}

7) I dos prompted to the "C;\eclipse\workspace\VegasVideo\bin" directory and typed the following:

ikvmc -out:VegasHello.dll - target:library -reference:Sony.Vegas.dll -reference:mscorlib.dll -reference:Interop.VegasCOM.dll Demo/VegasHello/Main.class

I got a minor warnng about referencing to the ikvm.gnu.classpath.dll, but the result was a VegasHello.dll in the bin directory.

8) I copied this VegasHello.dll file into the C:\Program Files\Sony\Vegas 5.0\Script Menu\Hello directory. I also copied the VegasHello.dll and VegasHello.js.config fles from the Peachrock tutorial

VegasHello.js follows:

import System;
import System.Windows.Forms;
import Demo.VegasHello;
try {
Main.Start(Vegas, ScriptFile);
} catch (e) {
MessageBox.Show("JScript: " + e.ToString() );
}

VegasHello.js.config follows:

<?xml version="1.0"?>
<ScriptSettings>
<AssemblyReference IsLocal="true">VegasHello.dll</AssemblyReference>
</ScriptSettings>


9) As discussed in the bottom paragraph at this link, rather than copying umpteen large files into every script directory, we should add two dlls to the "Global Assembly Cache". In WinXP, open the Control Panel, then the Administrative Tools, launch the Microsoft .NET Framework 1.1 Configuration, select "Manage the Assembly Cache" then "Add an Assembly to the Assembly Cache" to bring up the file dialog, then go to "c:\Progs\ikvm.bin and add the IKVM.GNU.Classpath.dll and IKVM.Runtime.dll. (Alternatively you could copy these two files nto each script directory.)

11) I think that's about it. Run the script and you'll see a "Got here" message box then the Vegas Version="" message box.

And that's good enough for me, for now. I've proved that I can write a script with a simple entry point into java, and I can access the Vegas instance from java As for the rest, we shall see...
rcampbel wrote on 2/21/2005, 6:25 PM
Congrats on getting it to work! I too was a Java programmer before delving into C#. I found that the C# language was pretty close to Java (except for properties instead of getters/setters), but the .Net class libraries are very different from Java.

Since you have to use the .Net class libraries even with Java, you still have to learn the .Net API, so it sounds like the main benefit is already being familiar with the development environment, correct?

Good luck,

Randall
RichMacDonald wrote on 2/21/2005, 9:14 PM
>I found that the C# language was pretty close to Java (except for properties instead of getters/setters), but the .Net class libraries are very different from Java.

I think C# is a better language than java, as I think you do to. I have some reasons for wanting to do this in java, though:

1) I'm a java developer at work and I cannot afford to maintain two libraries of code. Or is it that I don't want to have to start over?

2) I'm doing video/image processing at work, e.g., displaying surveillance video on a PocketPC, detecting motion, etc, and I want to leverage that expertise/library.

3) The IDE is more important than the language, imho and Eclipse is excellent.

------
As an update, I'm currently unable to debug, so this is the next challenge. If I could just instantate Vegas from another .NET app and get a reference to it...