1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
25 #include "sal/types.h"
26 #include "osl/thread.h"
27 #include "rtl/ustring.hxx"
28 #include "rtl/byteseq.hxx"
29 #include "jvmfwk/framework.h"
32 static bool hasOption(char const * szOption
, int argc
, char** argv
);
33 static OString
getLD_LIBRARY_PATH(const rtl::ByteSequence
& vendorData
);
34 static bool findAndSelect(JavaInfo
**);
37 "\njavaldx is necessary to make Java work on some UNIX platforms." \
38 "It prints a string to std out that consists of directories which " \
39 "have to be included into the LD_LIBRARY_PATH variable.The setting of " \
40 "the variable usually occurs in a shell script that runs javaldx.\n" \
41 "The directories are from the chosen java installation. \n" \
45 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
49 if( hasOption("--help",argc
, argv
) || hasOption("-h", argc
, argv
))
51 fprintf(stdout
, HELP_TEXT
);// default
54 javaFrameworkError errcode
= JFW_E_NONE
;
55 sal_Bool bEnabled
= sal_False
;
56 errcode
= jfw_getEnabled( & bEnabled
);
57 if (errcode
== JFW_E_NONE
&& bEnabled
== sal_False
)
59 //Do not do any preparation because that may only slow startup time.
62 else if (errcode
!= JFW_E_NONE
&& errcode
!= JFW_E_DIRECT_MODE
)
64 fprintf(stderr
,"javaldx failed!\n");
68 JavaInfo
* pInfo
= NULL
;
69 errcode
= jfw_getSelectedJRE( & pInfo
);
71 if (errcode
!= JFW_E_NONE
&& errcode
!= JFW_E_INVALID_SETTINGS
)
73 fprintf(stderr
,"javaldx failed!\n");
79 if (!findAndSelect(&pInfo
))
84 //check if the JRE was not uninstalled
85 sal_Bool bExist
= sal_False
;
86 errcode
= jfw_existJRE(pInfo
, &bExist
);
87 if (errcode
== JFW_E_NONE
)
89 if (!bExist
&& !findAndSelect(&pInfo
))
94 fprintf(stderr
, "javaldx: Could not determine if JRE still exist\n");
99 OString sPaths
= getLD_LIBRARY_PATH(pInfo
->arVendorData
);
100 fprintf(stdout
, "%s\n", sPaths
.getStr());
101 jfw_freeJavaInfo(pInfo
);
103 catch (const std::exception
&)
105 fprintf(stderr
,"javaldx failed!\n");
112 OString
getLD_LIBRARY_PATH(const rtl::ByteSequence
& vendorData
)
114 const sal_Unicode
* chars
= reinterpret_cast<sal_Unicode
const *>(vendorData
.getConstArray());
115 sal_Int32 len
= vendorData
.getLength();
116 OUString
sData(chars
, len
/ 2);
117 //the runtime lib is on the first line
119 OUString aToken
= sData
.getToken( 1, '\n', index
);
122 OUStringToOString(aToken
, osl_getThreadTextEncoding());
126 static bool hasOption(char const * szOption
, int argc
, char** argv
)
129 for(sal_Int16 i
= 1; i
< argc
; i
++)
131 if( ! strcmp(argv
[i
], szOption
))
140 static bool findAndSelect(JavaInfo
** ppInfo
)
142 javaFrameworkError errcode
= jfw_findAndSelectJRE(ppInfo
);
143 if (errcode
== JFW_E_NO_JAVA_FOUND
)
145 fprintf(stderr
,"javaldx: Could not find a Java Runtime Environment!\n");
148 else if (errcode
!= JFW_E_NONE
&& errcode
!= JFW_E_DIRECT_MODE
)
150 fprintf(stderr
,"javaldx failed!\n");
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */