Building JUNG library with Maven - java

I'm new to Java and I want to build the JUNG 2.2 library using Maven, because this seems to be the standard way to build it. I've never used Maven before but it was not difficult to install and running mvn clean install worked without a problem.
Now, JUNG appears to have 6 sub-packages (API, Implementations, Algorithms, I/O, Visualization and Samples), each one with its directory (e.g. jung-api is the directory for the API package). The install target produces some Java .class files and puts them in the target/classes/ directory of each of the 6 directories.
I am puzzled with what to do next in order to use the library: do I just need to add the jung directory path to my CLASSPATH variable in the shell? but then I would have to import jung-api.target.classes.edu.uci.ics.jung.graph.* which is not only extremely long to type but also has a “-”, which the Java compiler doesn't want in a package's name! And even when I bypass the “-“ problem I get a runtime error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/base/Supplier
I'm using javac 1.8.o_91 on OS X 10.9.5.

The solution I found was to jar -xf each one of the .jar files in the jung-samples directory, and then move all the directories extracted by jar in a place which I added to my CLASSPATH variable. At least this seems to work.

Related

ANTLR / java / SDK generate-compile-execute sequence fails on Windows10 command window

I'm trying to compile a tiny hello.g4 grammar file using ANTLR 4.8 on Windows 10. My end target is to run ANTLR inside of Visual Studio.
The problem is that something is happening to prevent me from running the generate, compile, execute sequence with constant PATH settings. I don't know enough about how Java works to determine what is wrong (or how to fix it).
The location of the latest Java SDK 14.0 files:
c:\program files\...\sdk 14.0\(a pile of files including java.exe and javac.exe)
The original Java 8 runtime location:
c:\program files (x86)\common files\Oracle\java\javapath (three files java.exe, javaw.exe, etc.)
The classpath points to my working directory (where the ANTLR java files are generated) and to the ANTLR jar file itself. The ANTLR complete jar file is in the same directory as everything else. The examples below both use the same CLASSPATH setting.
CLASSPATH=.;c:\dev\bin\antlr-4.8-complete.jar
I do not have JRE_HOME or JAVA_HOME or any such environment variables set. Only CLASSPATH and PATH.
A WORKING SEQUENCE
This sequence works (indicating that the grammar and tools work)
PATH=(the Java8 runtime location);(the SDK location);... other paths
// generate the parser with the SDK path explicitly (with PATH=Java8 in front)
"C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.Tool hello.g4
// now switch the PATH variable to put the SDK first
// this compiles and runs the generated files successfully
PATH=(the SDK location);(the Java8 runtime location);... other paths
"C:\Program Files\Java\jdk-14.0.2\bin\javac" hello*.java
"C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.gui.TestRig %*
A FAILED SEQUENCE
CLASSPATH=.;c:\dev\bin\antlr-4.8-complete.jar (unchanged from above)
If the SDK is first in the path, generation works, but compilation fails
PATH=(the SDK location);(the Java8 runtime location);... other paths
OK: "C:\Program Files\Java\jdk-14.0.2\bin\java" org.antlr.v4.Tool hello.g4
FAILS: "C:\Program Files\Java\jdk-14.0.2\bin\javac" hello*.java
Hundreds of errors are generated, among them ones like this.
fooLexer.java:6: error: package org.antlr.v4.runtime does not exist
import org.antlr.v4.runtime.*;
^
I have no files named org.antlr.v4.runtime; could it/they be in the antlr.4.8.complete.jar file or something?
The Java8 PATH must be first for the generation phase
The SDK PATH must be first for the compilation and execution phases.
I'm also just using a command line window to run the commands - no IDE is involved.
Can anyone tell me how to fix things so that I can run a generation, compile, execution cycle without flipping my PATH variable? Thank you. PS. I have read half a dozen potentially "duplicate" questions here on SO, but they all involve a different setup (with IDEs) and nothing I tried from them worked for me.
Since ANTLR 4.6.5-beta001 release of ANTLR4 for C#, it is possible to use ANTLR4 directly within VS to precompile grammar files to C#, then compile and run your app. All you need is install the nuget package. Today, you have several choices
"ANTLR4 Standard" version
"ANTLR4CS" optimized version
It works out of the box. Update your grammar, save it, it gets precompiled. Hit F5 and you are running it! More info in this answer
If you need to fiddle the inside workings of the build, check this link
To solve your path problem, the code generator installed by the AntlrCS package is antlr4.exe: jar is stored inside, so no more path problems. I have a simple setup that precompiles grammars outside VS. Working from the Antlr4Dy folder, I generate C# code in the src sub folder. The Code generator package is downloaded into the given folder, along with Antlr4.exe
"C:\Users...\source\repos\Antlr4Dy\packages\Antlr4.CodeGenerator.4.6.6\tools\net45\Antlr4.exe" C:\Users...\source\repos\Antlr4Dy\Speak.g4 -o src -Dlanguage=CSharp -package Antlr4x -no-listener -visitor

Javac does compiling always have to be from top of package? (Passing path of package to javac ?)

When compiling java code the I have been told the compiler must be run from the top of the package.
That is if I am trying to compile Test.java which is in tools.testing I have to first set the top of the package hierarchy, the folder containing /tools in order for it to work.
The class I am trying to compile uses another class contained in the same package and as such passing the full path of the code to the compiler prevents it from seeing the other class (as it doesn't search current directory and instead searches for the package inside of itself: ./tools/testing when it is already in /tools/testing )
I wanted to know if this was always the case or if there was a way to, for example: provide the path to the top of the package (since passing full path will not work for me) as an argument of the javac command or something similar ?
Thanks !
You should use an Integrated Development Environment (IDE) like IntelliJ, Eclipse or Netbeans. In an IDE you can create a Java project which has a directory acting as the 'source root'.
If you use Maven as your build tool the default location for such a directory is /src/main/java/ (this is the de-facto standard for Java projects at this time).
The IDE will automatically compile your Java files for you and allow you to run them easily during development.
If you want to run the application stand-alone you have to package it in some way. One simple and effective way is to generate a .jar file which contains all the .class files and other files you need (like images, .properties files etc). If you specify a pom.xml file for your project (that's Maven again) and set the packaging to jar Maven will automatically create a .jar file for you. You can even make the .jar file runnable with some additional settings.
See also this answer for some more info about packaging.

Noobie can't get Java classpath working

I am fairly new to java and just can't get my java application which relies of external libraries working...
I have two libraries and an application, all exported from eclipse as jars.
NOTE: I don't want them all exported into the same jar, as I wish to re-use the two libraries elsewhere.
enix.lib.common.jar (library 1)
enix.lib.events.jar (library 2)
enix.cmd.events.jar (console application)
When I run:
java -jar enix.cmd.events.jar
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: enix/lib/events/errors/EventLogNotAvailableException
Which obviously means it doesn't have the path to the enix.lib.events.jar (which if renamed to a zip file contains the file enix/lib/events/errors/EventLogNotAvailableException.class) - I then set the classpath in various ways like so:
java -cp ".;*.jar;enix.lib.events.jar" -jar enix.cmd.events.jar
But I get the same error. :(
I also have a GUI app called enix.gnome.events.jar which relies on various jars in /usr/share/java and /opt/libs/jars.
Could someone please explain what I am getting wrong and why, I would be most grateful! THANKS!
*.jar doesn't work, unfortunately. You can use wildcards in java classpaths, but only by putting all your jars in a directory and telling the classpath to use every jar that's there (see this).
I recommend, though, just listing out every jar that you need explicitly. Conventionally you'll see that most applications list every single jar.
When compiling/running java apps with multiple jars, I've found the easiest way is to just add the applicable jars right to my classpath. That way, when you compile or run the program, all of the applicable jars are available.
See http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html for setting the classpath in Windows/Unix/Linux.

Where do java packages live on a linux system? Package org.json does not exist Error using javac [duplicate]

This question already has answers here:
What is a classpath and how do I set it?
(10 answers)
Closed last year.
I am trying to compile a library I wrote, using javac and I am getting the error: package org.json does not exist. My program includes org.json.JSONArray and org.json.JSONException.
I have this package installed on my computer because I have successfully compiled android apps that import org.json libraries. I'm pretty sure all I have to do is specify a -classpath but I have been unable to find where these files live on my system (ubuntu 10.10 64-bit sun-java6).
Having been unable to find these on my own system I downloaded the org.json files from here, but I was unable to compile them individually because they were co-dependent on each other.
So I have a couple questions:
Does anyone know where the org.json
package lives from android sdk
install?
Where might I find a tutorial
explaining these basic concepts
regarding compiling, and javac.
Whatever external jars you need to compile with should be on the classpath when you compile. The most non-invasive way to do this is do add these items to the javac command line such as
javac -classpath /path/to/json.jar;. -g YourClass.java
or more likely if you use an IDE, add these jars to your referenced jars of the project in your IDE.
It usually isn't a good idea to pollute the global $CLASSPATH variable, as this then gets pulled in for everything you do with java, which may cause unintended conflicts.
Wherever you like. What you need to do is examine your CLASSPATH variable, and make sure it includes the directory with your library.
Here's the first thing:
$ echo $CLASSPATH
and you'll see your classpath as it is.
Now you need to find the jar file containing the org.json; consult the documentation, but it may be something as simple as json.jar. On most LINUX systems you can then just run
$ locate json.jar
And you'll get a path name for the jarfile. Make sure that path is part of your CLASSPATH and you'll be in fat city.
Oh, and the "Getting started" tutorials at Sun Oracle are the easiest place to start.
Actually, having looked at the files, they may not be packaged as a jar file. In that case, you want to put them into your sources starting at some top directory (src in this example.)
/src
/org/json/ ... put the json files here
... put your files here
and when you compile, they'll all be included, which will resolve all the dependencies.
Again, the place to look for first steps is that tutorial.
use "java" command instead of "javac"

Unable to run JAR file on another PC. Possible issue with Environment Variables?

I've built a JAR file and it executes fine on my PC (XP) which has Eclipse installed. It also works on another PC, which also has Eclipse.
I've tried running it on another PC(XP) that does not have Eclipse. Though it contains the JDK and multiple JRE. The JAR file just does not execute by clicking or from the command prompt.
I am not entirely sure, but my best guess is the Environment Variables are not set properly. Here is the error I receive from the command prompt:
Exception in thread "main" java.lang.NoClassDefFoundError: ...
Any help would be appreciated.
It must be a CLASSPATH issue.
The stacktrace should also say which class it failed to find. Once you have that, then find which jar has that class. Then add that jar file to your classpath or add it to the classpath env variable.
This is likely a classpath issue as others have said.
One thing to note is how your jar is constructed. You have a number of options in the dialog for exporting a runnable jar;
Extract classes into jar
Zip dependencies into the jar - creates jar-in-jar-loader.jar inside the jar.
Place jars in a subdirectory next to the jar.
Depending on what you have chosen for this depends on how the jar will behave. If the classes are extracted, dependent classes not in the JDK should be on the classpath. I'd recommend this course of action as it is simpler.
Now, the question is - are you using a dependency on your classpath not in the build dependencies of the eclipse project? If so, it won't be packed with / zipped into / put next to the jar because eclipse doesn't know about it (but java will still find it on your system because it's on the classpath). Also, if you've saved an ANT script and updated the build path in eclipse, eclipse won't update that ANT script - that is generated once only.
Environment variables are not considered when invoking a jar file when clicking on it (equivalent to running javaw -jar your.jar).
I'm pretty sure that it doesn't work on your first PC outside of Eclipse either.

Categories

Resources