Javac cannot access classes within libraries - java

I'm currently having trouble with compiling a project I'm working on.
I use some libraries, namely lwjgl, slick and jorbis. I've added them to my classpath and I am compiling with the following command
javac -g -classpath ".:./lib/lwjgl.jar:./lib/slick.jar:./lib/jorbis-0.0.17-2.jar" -d ./bin #files
The files file is simply a list of every class I'd like to compile.
The compiler then throws a lot of errors like:
foo\bar.java:25 cannot find symbol
bar\foo.java:3 package does not exist
What's especially strange is that compilation worked on a previous project where I only used one library instead of multiple.
I use Sublime Text 3 and bash on Linux Mint to code / compile my work (if that helps.)
All help is very welcome =]
[EDIT]:
I got the project to compile now.
I also tried to compile this on Windows (using git bash) and I get the same errors, but not on Linux anymore. I'll try to compile using a batch script on Windows, but at least I can compile it =]
Also works when I pack the finished project into a .jar.
Thanks y'all.

Related

How to compile Java class with .jar dependencies on Linux command line

I'm a CS Student working with my first .jar file and I'm having trouble getting my class that's dependent on it to compile. I'm using VSCode as my code editor, and I've added the .jar file to the "Referenced Libraries" section of my java project. The class I'm trying to run is a fairly simple class generated from our textbook, with an accompanying .jar file that has a bunch of custom libraries on it. I can see the contents of the jar in the VSCode java project browser, but when I try to compile I get import and symbol not found errors. I usually compile and run my projects in the terminal window (running Ubuntu 18.04 over Windows using WSL) by simply typing "javac MyClass.java". I would expect that if the jar has been loaded into VSCode, then it should compile the same way, but that doesn't appear to be the case. I've tried the recommended syntax "javac -cp /lib/myjar.jar MyClass.java" and "javac -jar /lib/myjar.jar MyClass.java" but neither works for me.
I'm fairly certain there's something simple I'm missing, as I don't have any experience working with jars, and certainly not in VSCode. Anyone out there that has an idea what I'm missing?

Can't compile a module in java 9 using cmd

Ok, so I'm trying to learn java 9 trough a book I bought, but when I try to compile a module trough the cmd, I get this error message. image of the cmd compile error
I have been trying to understand what is happening for the last 2 days.
The problem in the screenshot you posted is that --module-source-path requires a path, identifying where the modules' source files are, so that it can compile them. Since you haven't specified where the source files are, the compiler is complaining about it.
If the project structure is
modulename/src/main/java/module-info.java
modulename/src/main/java/com/whatever/Main.java
then the command to compile it would be (run from the parent of project)
javac -d outputfolder --module-source-path "./*/src/main/java" -m modulename
The * symbolizes the module's directory.
I suggest you go through the examples in your book by following the tutorial before experimenting.

Java - how to run console program in windows

Hi I wrote wordquiz program on Java. Using Eclipse in Unix.
In my linux machine it works fine.
Here is a source code https://github.com/HighlanderGe/Words
So, only basic package is used.
In windows compilation of such code as jar doesn't run. Neither in Mac.
As I guess problem is that in linux it is made to run from console, and console is comething very native for linux, but in Windows and I think in Mac too, cmd should be called, and from there it somehow to run.. but I bet cmd has no idea what is java. So some java console is needed for it?
The problem is not with the Mac or Windows, the problem is that you did not setup your workspace in Eclipse the same way on your different computers.
You can build your program on the command line in all environments in the same way. You just have to know the right steps.
First of all, there's an error in your code on line 25 in WordDatabase. Instead of:
dictionary = new ArrayList<>();
it should be:
dictionary = new ArrayList<String>();
After that, you can build your code like this:
javac -d . *.java
And run it like this:
java wordquizz/Wordquizz
This should work in any system that has Java, you just need to figure out how to setup your workspace in Eclipse the same way on your different computers.
UPDATE
I forked and converted your project to a Maven project:
https://github.com/janosgyerik/StackOverflow-Words
After you clone this to your PC, you can import into Eclipse using the File | Import... menu, and then Existing Maven projects option. It should work on all operating systems.
Maven is a recommended tool for building Java projects and it's a good thing to learn. After you install maven, you can build the project with:
mvn compile
You can package the project into a jar file with:
mvn package
You can run your code with either of these commands:
# needs 'mvn compile' first to generate classes
java -cp target/classes/ wordquizz.Wordquizz
# needs 'mvn package' first to generate the jar
java -cp target/wordquizz-1.0-SNAPSHOT.jar wordquizz.Wordquizz
If you like these improvements, merge from my repo soon. I won't keep it forever, I will delete it at some point.
UPDATE 2
To make a jar executable, you need to add inside a manifest file like this:
Main-Class: wordquizz.Wordquizz
You create the jar file with a command like this:
jar cvfm package.jar manifest.txt wordquizz/*.class
I updated my GitHub repository, so that now if you run mvn package, it will automatically add the right manifest, and the generated jar file will be executable.
If you installed java under windows you have to adjust the Path-variable, so the cmd knows where the java executable is. A good tutorial on how to set this up you can find here:
http://docs.oracle.com/javase/tutorial/essential/environment/paths.html
After that you can simple go to the directory your source code is in and use the same commands as under linux to compile and run your application.

Imports, jars, and heart Ache

first off let me start by saying I am completely new to Java, but to give you an idea of how new; I started reading lots of books, examples and so forth and began programming Java using Eclipse about 2 months ago. However, I found a really cool bit of advise about using notepad and the terminal to program instead. Kinda crazy for a newbie to go the hard route, but I love a challenge and I'm serious about learning.
So, In Eclipse I had a really good grasp of how to import, add jars compile etc. When I started using pico and using the terminal (I'm running ubuntu) to compile all went really well, until I wanted to use packages. I've spent two days pulling my hair out because no matter what I do I can't figure it out.
I'm trying to use the acm.jar (which I have many times in Eclipse) however I'm completely lost on how to use it when compiling from the javac in terminal.
So what I'm asking for, is for someone to explain the process getting my jar file to work.
All I'm using to create my java programs is the pico (or notepad) and the javac in the terminal.
To compile and run a java class using external libraries, you have to add that library to the classpath. The classpath is the set of places where the java compiler and the JVM look to find any external libraries/classes that it needs during the process of compiling/executing.
Setting the classpath can be done in 2 ways:
Set an environment variable called CLASSPATH
Set it when your run javac/java
Setting the classpath when running javac/java is done like this:
javac -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass.java
To run:
java -cp path/to/jar1:path/to/jar2:path/to/jar3:path/to/dirContainingClasses
yourMainClass
: is used as a separator on Linux, for windows use ;
Assuming your source files are in src. Assuming you want your compiled classes to be in classes. Assuming your source files reference classes that are in lib/acm.jar:
javac -cp classes:lib/acm.jar -d classes src/com/foo/bar/MyClass.java
will compile the class com.foo.bar.MyClass and put the generated MyClass.class file in classes/com/foo/bar.
You need the acm.jar file in the classpath. That's what the -cp lib/acm.jar option does. You also need classes in the classpath, because MyClass probably references other classes that you have already compiled and that are in your classes directory.
To run your class, it has to be in the classpath, and acm.jar as well:
java -cp classes:lib/acm.jar com.foo.bar.MyClass
As you see, the classpath contains jar files, and directories containing the folder hierarchy which matches the package hierarchy.
I wouldn't use javac from the command line directly, though. Try using a real build tool, that will build all your classes at once, like Gradle or Ant. Maven is also very popular, but I hate it with passion.

javac: package org.apache.derby.client.am does not exist

I'm trying to compile a java program that is using JavaDB/Derby. On the command line I can run
java org.apache.derby.tools.sysinfo
without errors, but the following line in several of the files causes the error in my question title:
import org.apache.derby.client.am.SqlException;
causes
package org.apache.derby.client.am does not exist
I've done a fresh installation of JavaDB, but I don't think that matters. I've compiled this project once before, and I KNOW I didn't have JavaDB installed. I just had a directory at the top level of the project folder called lib with all of derby's .jar files inside. And I'm pretty sure I didn't have to set any environment variables either.
How can I fix this error? If I need to provide any more information, I will be happy to do so.
I'm using Windows 7 and jdk1.7
Sounds like you have an issue with the JavaDB JARs not being on your classpath. Make sure you specify them using -cp or -classpath on your javac command.

Categories

Resources