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 .
20 #include <sal/config.h>
29 #include <sal/types.h>
30 #include <osl/thread.h>
31 #include <rtl/ustring.hxx>
32 #include <rtl/byteseq.hxx>
33 #include <jvmfwk/framework.hxx>
36 static bool hasOption(char const * szOption
, int argc
, char** argv
);
37 static OString
getLD_LIBRARY_PATH(const rtl::ByteSequence
& vendorData
);
38 static bool findAndSelect(std::unique_ptr
<JavaInfo
>*);
41 "\njavaldx is necessary to make Java work on some UNIX platforms." \
42 "It prints a string to std out that consists of directories which " \
43 "have to be included into the LD_LIBRARY_PATH variable.The setting of " \
44 "the variable usually occurs in a shell script that runs javaldx.\n" \
45 "The directories are from the chosen java installation. \n" \
49 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc
, argv
)
53 if( hasOption("--help",argc
, argv
) || hasOption("-h", argc
, argv
))
55 fprintf(stdout
, HELP_TEXT
);// default
58 bool bEnabled
= false;
59 javaFrameworkError errcode
= jfw_getEnabled( & bEnabled
);
60 if (errcode
== JFW_E_NONE
&& !bEnabled
)
62 //Do not do any preparation because that may only slow startup time.
65 else if (errcode
!= JFW_E_NONE
&& errcode
!= JFW_E_DIRECT_MODE
)
67 fprintf(stderr
,"javaldx failed!\n");
71 std::unique_ptr
<JavaInfo
> aInfo
;
72 errcode
= jfw_getSelectedJRE(&aInfo
);
74 if (errcode
!= JFW_E_NONE
&& errcode
!= JFW_E_INVALID_SETTINGS
)
76 fprintf(stderr
,"javaldx failed!\n");
82 if (!findAndSelect(&aInfo
))
87 //check if the JRE was not uninstalled
89 errcode
= jfw_existJRE(aInfo
.get(), &bExist
);
90 if (errcode
== JFW_E_NONE
)
92 if (!bExist
&& !findAndSelect(&aInfo
))
97 fprintf(stderr
, "javaldx: Could not determine if JRE still exist\n");
102 OString sPaths
= getLD_LIBRARY_PATH(aInfo
->arVendorData
);
103 fprintf(stdout
, "%s\n", sPaths
.getStr());
105 catch (const std::exception
& e
)
107 std::cerr
<< "javaldx failed! " << e
.what() << std::endl
;
114 OString
getLD_LIBRARY_PATH(const rtl::ByteSequence
& vendorData
)
116 const sal_Unicode
* chars
= reinterpret_cast<sal_Unicode
const *>(vendorData
.getConstArray());
117 sal_Int32 len
= vendorData
.getLength();
118 OUString
sData(chars
, len
/ 2);
119 //the runtime lib is on the first line
121 OUString aToken
= sData
.getToken( 1, '\n', index
);
124 OUStringToOString(aToken
, osl_getThreadTextEncoding());
128 static bool hasOption(char const * szOption
, int argc
, char** argv
)
131 for(int i
= 1; i
< argc
; i
++)
133 if( ! strcmp(argv
[i
], szOption
))
142 static bool findAndSelect(std::unique_ptr
<JavaInfo
> * ppInfo
)
144 javaFrameworkError errcode
= jfw_findAndSelectJRE(ppInfo
);
145 if (errcode
== JFW_E_NO_JAVA_FOUND
)
147 fprintf(stderr
,"javaldx: Could not find a Java Runtime Environment!\n");
150 else if (errcode
!= JFW_E_NONE
&& errcode
!= JFW_E_DIRECT_MODE
)
152 fprintf(stderr
,"javaldx failed!\n");
159 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */