I decided I was going to "man up" and figure out how to compile a java program with an external dependency from the command line instead of relying on an IDE-- the DOS command line, to be more specific.
I ran into a few problems:
1. The external dependency was given to me as a java file. I experimented compiling it as a .jar, but I wasn't sure how to import a class from a .jar, so I ended up compiling it into a class.
2. When I tried to run the file, I got an error saying that the class had been compiled with a different version of Java than my JRE. The Internet told me to check my path variable for Java. It sure looked like it was pointing to the latest JRE (and the same version of Java as my compiler). I asked the Internet again and found the following command:
for %I in (java.exe) do @echo %~$PATH:I
I'm not exactly sure what the syntax of that magic command is (intuitively it's returning the path that executes when I run the "java" command), but it was certainly revealing: I had some old system level install of Java that I'd done before I installed the most recent JDK. I removed that old install, and the error went away.
3. I was able to compile the java program and feed it the correct path for the library -- but when I tried to run it, I needed to feed it the path for the library again and I couldn't figure out how to do it. I knew it involved the -cp argument. I thought I was just supposed to give it the path to the dependency. After a lot of experimenting, I realized I didn't understand exactly what -cp does (what the classpath is in Java). So I looked it up, and as far as I can tell now, the classpath is where Java looks for classes (duh). So if I have a dependency class in one directory and a main class in another directory, I have to give Java two classpaths. The way to do that -- add a semicolon:
java -cp <path to directory where the library is>;<path to directory where the main class is> <main class>
or in my case
java -cp libraries;out\production\AlgorithmsChapter1 Drawing
So what this says is run a java program called Drawing and look for classes in libraries (where the dependency is) and in out\production\AlgorithmsChapter1.
I should know all of these things before I start fumbling around writing Java programs. But it's still worth learning -- even if I have to learn it the hard way.
I ran into a few problems:
1. The external dependency was given to me as a java file. I experimented compiling it as a .jar, but I wasn't sure how to import a class from a .jar, so I ended up compiling it into a class.
2. When I tried to run the file, I got an error saying that the class had been compiled with a different version of Java than my JRE. The Internet told me to check my path variable for Java. It sure looked like it was pointing to the latest JRE (and the same version of Java as my compiler). I asked the Internet again and found the following command:
for %I in (java.exe) do @echo %~$PATH:I
I'm not exactly sure what the syntax of that magic command is (intuitively it's returning the path that executes when I run the "java" command), but it was certainly revealing: I had some old system level install of Java that I'd done before I installed the most recent JDK. I removed that old install, and the error went away.
3. I was able to compile the java program and feed it the correct path for the library -- but when I tried to run it, I needed to feed it the path for the library again and I couldn't figure out how to do it. I knew it involved the -cp argument. I thought I was just supposed to give it the path to the dependency. After a lot of experimenting, I realized I didn't understand exactly what -cp does (what the classpath is in Java). So I looked it up, and as far as I can tell now, the classpath is where Java looks for classes (duh). So if I have a dependency class in one directory and a main class in another directory, I have to give Java two classpaths. The way to do that -- add a semicolon:
java -cp <path to directory where the library is>;<path to directory where the main class is> <main class>
or in my case
java -cp libraries;out\production\AlgorithmsChapter1 Drawing
So what this says is run a java program called Drawing and look for classes in libraries (where the dependency is) and in out\production\AlgorithmsChapter1.
***
I should know all of these things before I start fumbling around writing Java programs. But it's still worth learning -- even if I have to learn it the hard way.
Comments
Post a Comment