The Hybrid Max-Java Application

 

Learning Objectives:

  1. understand the flow of control in the Max example application
  2. understand the flow of control in the Java example application
  3. appreciate the difference between the two applications (even though the have the same output)
  4. understand the flow of control in the hybrid Max-Java example application
  5. be able to edit the java element of a hybrid Max-Java application, compile it, and run it using the following:
    1. the MaxMSP Runtime environment and the Mxj Quickie Development Environment
    2. the MaxMSP Runtime environment and the Eclipse IDE

 

 

 

1. Flow of Control (Max)

 

 

  1. create a new project in Eclipse called ÒclassesÓ

á      asd  IMPORTANT!!!  Follow these instructions exactly:
File -> New -> Java Project
Under ÒProject NameÓ type ÒclassesÓ
Under ÒProject LayoutÓ select ÒUse project folder as root for sources and class filesÓ

á      If you get the error ÒA project with this name already existsÓ then cancel.  Check the project folder (look under Z:/workspace-win32/ for a folder called classes).  If the folder has two sub-directories called ÒbinÓ and ÒsrcÓ then this means the project has been created already but with the project layout option that is incompatible with the MaxMSP set-up that we require.  Rename the project or delete it (depends on whether you have classes within it that you want to preserve) and re-create the project following the steps above.

 

  1. copy ExampleApplication1.maxpat into the folder Z:\workspace-win32\classes the following (from course wiki -> Resources )
  2. open the patch in Max RT and click on the bang object as indicated below.  Look at the MaxMSP console window (Window->Max)

 

ExampleApplication1.maxpat

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


                       

 

 

 

 

 

 

 

2. Flow of Control (Java)

 

  1. copy the file ExampleApplication1.java to your classes subdirectory.
  2. refresh the project.  Put the classes project in focus by clicking once on it (it should turn blue).  File->Refresh (or press the shortcut F5)
  3. run the app

 

 

 


 

3. Control Flow: Differences between Max and Java


 

Java VM running ExampleApplication1.class

 

á      Upon start up, the Java VM loads the class ÒExampleApplication1Ó (as well as all other classes that are additionally specified)

á      The VM looks for the special method in that class that is called ÒmainÓ

á      It invokes the first line of the main method

á      It invokes each line, one after the other

á      When the last line of the main method is finished, the Java VM stops running and closes

 


MaxMSP running ExampleApplication1.maxpat                             

 

á      Upon start up, it initialize all of its objects, which will start up the Java VM

á      Upon start up, the Java VM loads the class ÒExternal Mxj ObjectÓ (as well as all other classes that are additionally specified)

á      The VM looks for the special method in that class that is called ÒmainÓ

á      The ÒExternal Mxj ObjectÓ application is designed to initialize and then to stop and wait for incoming events

á      The plugged-in component specifies what should happen (if anything) when an incoming event is received

á      When an event is received, the method with the name corresponding to the eventÕs message gets invoked. 

á      The application is designed to keep running until Max is stopped.

 

 



 

 

4. Flow of Control (Max-Java Hybrid)

 

 

  1. copy the file HelloServices.java, ExampleApplication2.maxpat, and max.jar to your classes subdirectory.
  2. Go to Eclipse.  Refresh the project.  Put the classes project in focus by clicking once on it (it should turn blue).  File->Refresh (or press the shortcut F5). 
  3. Add max.jar to the build path of the project.
  4. Open HelloServices.java.  This will trigger a compile of HelloServices.
  5. open ExampleApplication2.maxpat using Max RT.
  1. click on the bang object, watch the Max console window for the output

 

 

 

 

 

 

  1. When the ÒExampleApplication2Ó patch is loaded, all of the objects get instantiated.
  2. In order to instantiate the mxj object, MaxMSP launches the Java Virtual Machine and starts running the ÒExternal MXJ objectÓ Java application. 
  3. About the ÒExternal MXJ objectÓ Java application: 
    1. it was designed and implemented by Cycling. 
    2. most of this application is not visible to us, but it is designed so that we can plug various components into it. 
    3. The component ÒHelloServicesÓ is an example of a pluggable component.  We will talk more about how this works later on in this course.  For now, you will be given the valid component and will be tasked with modifying the bodies of methods that already exist.
  4. Once the Java VM starts running, MaxMSP cannot stop it and cannot load and start running any java classes other than the one specified at start up.  This part sucks, as we will see in the next section. (But we can work around this.)

 

 

 


 


 

 

5. Modification of the Max-Java Hybrid (Eclipse)

 

 

1.     In Eclipse, make a change in the java code in HelloServices.java (e.g., instead of printing ÒHello!Ó to the console, print something else)

2.     Save the code.  Compilation will begin automatically.  You can assure yourself of this by checking the time stamp of the file HelloServices.class ; if it doesnÕt, have a look at Project Ý Build Automatically.  Ensure there is a check mark next to ÒBuild AutomaticallyÓ)

3.     Switch to MaxMSP.

4.     Close the current patch and re-open the current patch using one of the following:

a.     File Ý Open Recent (select patch from list) [not necessary to close first]

b.     Go to the file in the file browser and double click to (re)open it

Why do we need to do this? 
The Java Virtual Machine running within MaxMSP needs to be told to use the new version of the bytecode instead of the old version.  The only way to do this is to re-open the patch. If you neglect to do steps 5a and 5b, then the old version of the bytecode will remain loaded.  Any changes you made to the java application will not be put into effect.

5.     Re-open the patch ExampleApplication2.maxpat and run