java.tmbundle icon indicating copy to clipboard operation
java.tmbundle copied to clipboard

class can not find other classes (source path is not set)

Open macfreek opened this issue 15 years ago • 2 comments

Here is a patch that will set the source path to the directory of the source file during compiling. This allows a class to find other classes in the same directory. Currently, I'm getting errors that the class is not found.

The given fix also sets the folder to the root of the package directory, so it works fine with packages too. As an added fix, it prints the javac line, so it is clear what is going on.

diff --git a/Support/bin/java_compile_and_run.sh b/Support/bin/java_compile_and_run.sh
index 4e32e6c..26e9b75 100755
--- a/Support/bin/java_compile_and_run.sh
+++ b/Support/bin/java_compile_and_run.sh
@@ -12,13 +12,19 @@ shift
 output="$TMPDIR/tm_javamate.${TM_PID:-$LOGNAME}";
 mkdir -p "$output"

+PACKAGEDIR=${TM_JAVA_PACKAGE//./\/}    # replace . with / in package
+SOURCEDIR=$(dirname "$SOURCE")
+SOURCEDIR=${SOURCEDIR/%\/$PACKAGEDIR/} # remove package prefix from path
+
 if [ -n "$TM_JAVA_FILEGLOB" ]; then
-  "$TM_JAVAC" -d "$output" $TM_JAVA_FILEGLOB; rc=$?;
+  echo "$TM_JAVAC" -sourcepath "$SOURCEDIR" -d "$output" $TM_JAVA_FILEGLOB;
+  "$TM_JAVAC" -sourcepath "$SOURCEDIR" -d "$output" $TM_JAVA_FILEGLOB; rc=$?;
   if (($rc >= 1)); then exit $rc; fi
 fi

 if [[ "$SOURCE" != $TM_JAVA_FILEGLOB ]]; then
-  "$TM_JAVAC" -d "$output" "$SOURCE"; rc=$?;
+  echo "$TM_JAVAC" -sourcepath "$SOURCEDIR" -d "$output" $SOURCE;
+  "$TM_JAVAC" -sourcepath "$SOURCEDIR" -d "$output" "$SOURCE"; rc=$?;
   if (($rc >= 1)); then exit $rc; fi
 fi

@@ -28,6 +34,6 @@ then
    CLASS="$TM_JAVA_PACKAGE.$CLASS"
 fi

-echo CLASSPATH="$output:$CLASSPATH" "$TM_JAVA" -Dfile.encoding=utf-8 "$CLASS" $@;
-CLASSPATH="$output:$CLASSPATH" "$TM_JAVA" -Dfile.encoding=utf-8 "$CLASS" $@;
+echo CLASSPATH="$output:$CLASSPATH" "$TM_JAVA" -Dfile.encoding="utf-8" "$CLASS" $@;
+CLASSPATH="$output:$CLASSPATH" "$TM_JAVA" -Dfile.encoding="utf-8" "$CLASS" $@;
 exit $?;

macfreek avatar Aug 21 '10 20:08 macfreek

Oh, and I put quotes around utf-8; somehow, I got a message "don't know what the -8 flags means". I didn't examine the cause; the quotes fixed it.

macfreek avatar Aug 21 '10 20:08 macfreek

I hope my description is clear. Currently, if I try to use a class in the same folder or import a class in the same package, I get errors because the referred class can not be found. Like this:

/Users/freek/Code/Java/OSXAdapter/src/org/example/myapp/MyApp.java:17: cannot find symbol
symbol  : class OSXAdapter
location: package org.example.myapp
import org.example.myapp.OSXAdapter;
                         ^

this patch makes sure these classes can be found.

macfreek avatar Aug 21 '10 21:08 macfreek