Code covered by the BSD License  

Highlights from
Ideal Rankine Cycle Using Builder for Java

image thumbnail
from Ideal Rankine Cycle Using Builder for Java by David Forstot
Calculate Thermodynamic Properties of Ideal Rankine Cycle - Integrating MATLAB with Java

compileRankineJavaDemo
function compileRankineJavaDemo
%
% This function will compile the Rankine Java demo.
%

% Authored By: Elwin Chan
% Modified By: David Forstot

% Copyright 2006 The MathWorks, Inc.


% Test the architecture first
arch = computer;
if ( strcmp(arch,'PCWIN64')) % || ismac)
    error('MATLAB Builder for JAVA Language tests cannot be run on Win64 and Mac machines');
end

% get the settings
params = getRankineJavaDemoSettings;

% Do the compiling
setPathForJava;
doMcc(params);
doJavac(params);
buildJar(params);

disp(sprintf('\nRun this demo using the command: \n\t java -jar %s', params.FinalJarName));


%%
function setPathForJava
% Ensure that the system path has been set up for Java compilation.

%see if JAVA_HOME has been set as an environment variable.  If not set it.
jdkDirName = getenv('JAVA_HOME');
if(isempty(jdkDirName))
    % get the JDK location from the user
    jdkDirName = uigetdir(matlabroot, 'Please select JDK Location:');

    if(jdkDirName == 0)
        error('Cannot compile PortOptDemo without the JDK installed');
    end

    %strip off '\bin' if it is there
    if(strcmpi(jdkDirName(end-3:end), '\bin'))
        jdkDirName = jdkDirName(1:end-4);
    end

    %set the JAVA_HOME environment variable
    disp('Setting the JAVA_HOME environment variable');
    setenv('JAVA_HOME', jdkDirName);
end

% see if JAVA_HOME\bin is on the path - if not, add it.
syspath = getenv('PATH');
if isempty(strfind(lower(syspath), lower(fullfile(jdkDirName, 'bin'))))
    disp('Adding JAVA_HOME\bin to the system path')
    setenv('PATH', [syspath, ';' fullfile(jdkDirName, 'bin')]);
end

%%
function doMcc(buildParams)
% Use Builder for Java to compile up the Matlab code into a jar

%check if the output directory exists
if(exist(buildParams.MLOutputRoot, 'dir') ~= 7)
    mkdir(buildParams.MLOutputRoot);
end

% generate the mcc command
mcccmd     = ['mcc -v -d ' buildParams.MLOutputRoot, ' ' ...
    '-W "java:' buildParams.MLGeneratedComponentName ',' ...
    buildParams.MLGeneratedClassName '" ' buildParams.mfileList];

% Do the mcc to compile the Matlab code into a jar
disp('Compiling Matlab Code...');
[status mccout] = system(mcccmd);

if ( (status ~= 0) || (exist(fullfile(buildParams.MLOutputRoot, 'rankine_package.jar'),'file') ~= 2) )
    error('System and MCC command:\n%s\n\nOutput of the command:%s\n\n',mcccmd,mccout);
else
    disp(sprintf('\tDone'));
end

%%
function doJavac(buildParams)
% Compile the java code

%check if the output directory exists
if(exist(buildParams.compiledJavaPath, 'dir') ~=7)
    mkdir(buildParams.compiledJavaPath);
end

%set up the classpath
if ispc
    delim = ';';
else
    delim = ':';
end

classStr = { '.' delim, ...
    '"', fullfile(matlabroot,'toolbox','javabuilder','jar','javabuilder.jar'), '"', delim, ...
    '"', fullfile(buildParams.MLOutputRoot, [buildParams.MLGeneratedComponentName '.jar']), '"', delim, ...
    '"', fullfile(buildParams.demoJavaLibRoot, buildParams.demoJavaAdditionalJar1), '"', delim, ...
    '"', fullfile(buildParams.demoJavaLibRoot, buildParams.demoJavaAdditionalJar2), '"'};

classpath = strcat(classStr{:});

% Don't put quotes around the file list because javac doesn't like it.
javaCodeFileList = fullfile(buildParams.demoJavaPackagePath, '*.java');

% form the javac command for compiling the code
javaccmd = ['javac -classpath ' classpath , ' -d "', buildParams.compiledJavaPath, '" ', javaCodeFileList];

% do the javac command
disp('Compiling Java Code...');
[status javacout] = system(javaccmd);

if ( (status~=0))
    error('JAVAC command used to compile the client application :\n%s\n\nOutput of javac command :\n%s\n',javaccmd,javacout);
else
    disp(sprintf('\tDone'));
end

%%
function buildJar(buildParams)
%Build the jar

%check if the output directory exists
if(exist(buildParams.FinalOutputRoot, 'dir') ~=7)
    mkdir(buildParams.FinalOutputRoot);
end

%first copy javabuilder.jar and the ML-generated .jar and .ctf into the
%final output folder so that everything can be bundled into a final jar
javaBuilderFilename = 'javabuilder.jar';
jarFilename = [buildParams.MLGeneratedComponentName '.jar'];
ctfFilename = [buildParams.MLGeneratedComponentName '.ctf'];

copyfile(fullfile(matlabroot,'toolbox','javabuilder','jar',javaBuilderFilename), ...
    fullfile(buildParams.FinalOutputRoot, javaBuilderFilename));
copyfile(fullfile(buildParams.MLOutputRoot, jarFilename), fullfile(buildParams.FinalOutputRoot, jarFilename));
copyfile(fullfile(buildParams.MLOutputRoot, ctfFilename), fullfile(buildParams.FinalOutputRoot, ctfFilename));

% form the jar command
jarcmd = ['jar cmf ', fullfile(buildParams.demoJavaCodeRoot, 'manifest.mf '), ...
    fullfile(buildParams.FinalOutputRoot, buildParams.FinalJarName) ' -C ', buildParams.compiledJavaPath, ' .'];

% do the jar command
disp('Creating JAR...');
[status, jarout] = system(jarcmd);

if( (status ~= 0) || (exist(fullfile(buildParams.FinalOutputRoot, buildParams.FinalJarName), 'file') ~= 2))
    error('JAR command:\n%s\n\nOutput of the command:%s\n\n', jarcmd, jarout);
else
    disp(sprintf('\tDone'));
end

Contact us at files@mathworks.com