1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) INRIA - Allan CORNET
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/
/*--------------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _MSC_VER
#include <windows.h>
#endif
#include "InitializeJVM.h"
#include "loadClasspath.h"
#include "loadLibrarypath.h"
#include "setgetSCIpath.h"
#include "getScilabJNIEnv.h"
#include "getScilabJavaVM.h"
#include "MALLOC.h"
#include "JVM.h"
#include "createMainScilabObject.h"
#include "scilabDefaults.h"
#include "localization.h"
#include "fromjava.h"
#ifdef _MSC_VER
#include "strdup_windows.h"
#endif
#include "catchIfJavaException.h"
/*--------------------------------------------------------------------------*/
static void DoLoadClasspathInEtc(char *sciPath);
static void DoLoadLibrarypathInEtc(char *sciPath);
/*--------------------------------------------------------------------------*/
BOOL InitializeJVM(void)
{
BOOL bOK = FALSE;
char *sciPath = NULL;
sciPath = getSCIpath();
if (!startJVM(sciPath))
{
#ifdef _MSC_VER
MessageBox(NULL, gettext("\nScilab cannot open JVM library.\n"), gettext("Error"), MB_ICONEXCLAMATION | MB_OK);
#else
fprintf(stderr, _("\nScilab cannot open JVM library.\n"));
#endif
}
else
{
DoLoadLibrarypathInEtc(sciPath);
DoLoadClasspathInEtc(sciPath);
if (!createMainScilabObject())
{
char *errorMsg = strdup(gettext("\nScilab cannot create Scilab Java Main-Class (we have not been able to find the main Scilab class. Check if the Scilab and thirdparty packages are available).\n"));
if (IsFromJava())
{
char *errorMsg2 = gettext("If Scilab is used from Java, make sure that your IDE (ex: Netbeans, etc) is not adding extra dependencies which could not be found at runtime.\n");
char *tempMsg = (char*)MALLOC(sizeof(char) * (strlen(errorMsg) + strlen(errorMsg2) + 1));
if (tempMsg)
{
strcpy(tempMsg, errorMsg);
strcat(tempMsg, errorMsg2);
FREE(errorMsg);
errorMsg = tempMsg;
}
}
#ifdef _MSC_VER
MessageBox(NULL, errorMsg, gettext("Error"), MB_ICONEXCLAMATION | MB_OK);
#else
fprintf(stderr, "%s", errorMsg);
#endif
if (errorMsg)
{
FREE(errorMsg);
errorMsg = NULL;
}
}
else
{
bOK = TRUE;
}
}
if (sciPath)
{
FREE(sciPath);
sciPath = NULL;
}
if (!bOK)
{
exit(1);
}
return TRUE;
}
/*--------------------------------------------------------------------------*/
static void DoLoadClasspathInEtc(char *sciPath)
{
char *classpathfile = (char*)MALLOC(sizeof(char) * (strlen(sciPath) + strlen(XMLCLASSPATH) + 1));
sprintf(classpathfile, XMLCLASSPATH, sciPath);
LoadClasspath(classpathfile);
if (classpathfile)
{
FREE(classpathfile);
classpathfile = NULL;
}
}
/*--------------------------------------------------------------------------*/
static void DoLoadLibrarypathInEtc(char *sciPath)
{
char *librarypathfile = (char*)MALLOC(sizeof(char) * (strlen(sciPath) + strlen(XMLLIBRARYPATH) + 1));
sprintf(librarypathfile, XMLLIBRARYPATH, sciPath);
LoadLibrarypath(librarypathfile);
if (librarypathfile)
{
FREE(librarypathfile);
librarypathfile = NULL;
}
}
/*--------------------------------------------------------------------------*/
BOOL ExecuteInitialHooks(void)
{
JNIEnv * currentENV = getScilabJNIEnv();
JavaVM * currentJVM = getScilabJavaVM();
jint result = (*currentJVM)->AttachCurrentThread(currentJVM, (void **) ¤tENV, NULL) ;
if (result == 0)
{
jclass cls = NULL;
cls = (*currentENV)->FindClass(currentENV, "org/scilab/modules/core/Scilab");
catchIfJavaException(_("Could not access to the Main Scilab Class:\n"));
if (cls)
{
jmethodID mid = NULL;
mid = (*currentENV)->GetStaticMethodID(currentENV, cls, "executeInitialHooks", "()V");
if (mid)
{
(*currentENV)->CallStaticVoidMethod(currentENV, cls, mid);
}
catchIfJavaException(_("Cannot execute initial hooks. Error:\n"));
return TRUE;
}
}
return FALSE;
}
/*--------------------------------------------------------------------------*/
|