New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Could not find or load main class when using symlinked run.sh #1272

Closed
guettli opened this Issue Aug 1, 2018 · 8 comments

Comments

Projects
None yet
4 participants
@guettli

guettli commented Aug 1, 2018

Affects PMD Version: pmd-bin-6.6.0

Description:

LANG=C run.sh cpd --files myfoo 
Error: Could not find or load main class net.sourceforge.pmd.cpd.CPD

java -version
openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11)
OpenJDK 64-Bit Server VM (build 25.171-b11, mixed mode)

Please let me know if you need further information.

OS: Ubuntu 16.04.4 LTS

@oowekyala

This comment has been minimized.

Member

oowekyala commented Aug 6, 2018

Does PMD work or does this happen only to CPD? Could you maybe paste the output of bash -x run.sh cpd --files myfoo here?

@guettli

This comment has been minimized.

guettli commented Aug 6, 2018

Here is the output of bash -x

LANG=C bash -x run.sh cpd --files modwincontrol_common/ 
+ readonly APPNAME=cpd
+ APPNAME=cpd
+ '[' -z cpd ']'
+ shift
+ case "${APPNAME}" in
+ readonly CLASSNAME=net.sourceforge.pmd.cpd.CPD
+ CLASSNAME=net.sourceforge.pmd.cpd.CPD
+ is_cygwin
+ case "$(uname)" in
++ uname
+ '[' -z ']'
+ readonly cygwin=false
+ cygwin=false
+ set_lib_dir
+ '[' -z ']'
++ dirname run.sh
+ local script_dir=.
+ local cwd=/home/modwork_vums_d/src
+ cd ./../lib
++ pwd -P
+ readonly LIB_DIR=/home/modwork_vums_d/lib
+ LIB_DIR=/home/modwork_vums_d/lib
+ cd /home/modwork_vums_d/src
+ check_lib_dir
+ '[' '!' -e /home/modwork_vums_d/lib ']'
+ convert_cygwin_vars
+ false
+ classpath=
+ cd ''
+ for jarfile in '"${LIB_DIR}"/*.jar'
+ '[' -n '' ']'
+ classpath='/home/modwork_vums_d/lib/*.jar'
+ cygwin_paths
+ false
+ java_heapsize_settings
+ local heapsize=
+ case "${heapsize}" in
++ jre_specific_vm_options
+++ java -version
++ full_ver='openjdk version "1.8.0_171"
OpenJDK Runtime Environment (build 1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11)
OpenJDK 64-Bit Server VM (build 25.171-b11, mixed mode)'
+++ echo openjdk version '"1.8.0_171"' OpenJDK Runtime Environment '(build' '1.8.0_171-8u171-b11-0ubuntu0.16.04.1-b11)' OpenJDK 64-Bit Server VM '(build' 25.171-b11, mixed 'mode)'
+++ sed -n '{
      # replace early access versions, e.g. 11-ea with 11.0.0
      s/-ea/.0.0/
      # replace versions such as 10 with 10.0.0
      s/version "\([0-9]\{1,\}\)"/version "\1.0.0"/
      # extract the major and minor parts of the version
      s/^.* version "\(.*\)\.\(.*\)\..*".*$/\1\2/p
  }'
++ java_ver=18
++ options=
++ '[' 18 -ge 90 ']'
++ echo
+ java -cp '/home/modwork_vums_d/lib/*.jar' net.sourceforge.pmd.cpd.CPD --files modwincontrol_common/
Error: Could not find or load main class net.sourceforge.pmd.cpd.CPD
@adangel

This comment has been minimized.

Member

adangel commented Aug 6, 2018

Thanks for the debug output.

It seems we detect the classpath directory wrong. We use "/home/modwork_vums_d/lib/*.jar" as the classpath for PMD/CPD....

Where did you extract pmd-bin-6.6.0.zip?
In which directory are you, when you execute "run.sh"?
Did you add "run.sh" to your PATH environment variable?
Did you maybe copy "run.sh" to some directory?

What we are trying to do in the script is: detect, where "run.sh" is located - wherever this is, this is the place, where pmd-bin-6.6.0.zip has been extracted, so we know, where the libs are.

Currently, we detect, that PMD/CPD is installed in "/home/modwork_vums_d/" - which probably is wrong...

@oowekyala

This comment has been minimized.

Member

oowekyala commented Aug 7, 2018

@adangel From the debug output, the current working dir is /home/modwork_vums_d/src, the script is in the same directory. PMD looks for the libs in ./../lib, which as expected is /home/modwork_vums_d/lib.

@guettli Does /home/modwork_vums_d/lib contain all the jars extracted from the lib directory of the PMD archive? These are required for PMD/ CPD to work.

Note that it would be more resilient to unpack the whole archive in a directory outside your sources (eg ~/.local/share or /usr/local), without altering the directory structure, and then add run.sh to your PATH (or define an alias).

@guettli

This comment has been minimized.

guettli commented Aug 13, 2018

I installed it in /usr/local: /usr/local/pmd-bin-6.6.0/

wouldn't it make sense if the script would look relative to its installation, instead of relative to the current working directory?

@adangel

This comment has been minimized.

Member

adangel commented Aug 13, 2018

wouldn't it make sense if the script would look relative to its installation, instead of relative to the
current working directory?

In theory, this should already be that way: Here we already use the script's directory to determine the path to the "lib"-folder, relative to the location of "run.sh".

One question remains: When you are executing the script, you are obviously in the directory /home/modwork_vums_d/src. You are calling the script via run.sh - but the full path is /usr/local/pmd-bin-6.6.0/bin/run.sh. How did you setup your environment, that just run.sh works?

From the debug script output, the script seems to be located in /home/modwork_vums_d/src/run.sh... is it maybe a symlink?

Edit: If /home/modwork_vums_d/src/run.sh is a symlink to /usr/local/pmd-bin-6.6.0/bin/run.sh, I think, I can reproduce the problem.

@guettli

This comment has been minimized.

guettli commented Aug 14, 2018

I guess the symlink is the reason:

===> ls -l /usr/local/bin/run.sh 
lrwxrwxrwx 1 root root 35 Aug  1 12:33 /usr/local/bin/run.sh -> /usr/local/pmd-bin-6.6.0/bin/run.sh

Thank you for this hint.

@adangel

This comment has been minimized.

Member

adangel commented Aug 14, 2018

OK, thanks for checking. I think, we should update the script, to support also symlinks.
Creating a symlink in /usr/local/bin is very common, I guess. This would affect not only CPD but PMD and all other tools as wel.
I'll update the title of this issue accordingly.

@adangel adangel changed the title from [core] Error: Could not find or load main class net.sourceforge.pmd.cpd.CPD to [core] Error: Could not find or load main class when using symlinked run.sh Aug 14, 2018

@adangel adangel added the a:bug label Aug 14, 2018

@adangel adangel changed the title from [core] Error: Could not find or load main class when using symlinked run.sh to [core] Could not find or load main class when using symlinked run.sh Aug 14, 2018

jsotuyod added a commit to Monits/pmd that referenced this issue Oct 15, 2018

@jsotuyod jsotuyod self-assigned this Oct 15, 2018

@jsotuyod jsotuyod added the has:pr label Oct 15, 2018

@jsotuyod jsotuyod added this to the 6.9.0 milestone Oct 15, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment