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

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

Related

VS Code runs .java files without the producing .class files

I have some basic java knowledge and i decided to switch from Intellij to VS Code. I know how to compile and run a java file (which may use other imported .java files) from the terminal, but i want to run a java program using the run icon inside vs code.
I'm not a 100% sure, but i think that in order to run a java program inside vs code you need a extension. For this reason i downloaded the Java extension pack. Note that i already have installed the latest jdk on my computer.
After i installed the plugin, i could run the program using the "run without debugging"/clicking in the run icon. However, no .class files are produced. I control+h but there isnt any hidden folder that vs code may drop the .class files.
How does the program run in the first place?
How can i config vs code in order to compile the java files before running the one that has the main function?
Basically, when you run a java file normally, you compile it with javac and then run it with java (or something along those lines, there are many different variations of possible ways to do it). If you go ahead and try, it is possible to run java filename.java and skip the normal javac step. In the case of VSCode, it will differ based on what extensions you have, but some may run the code with a simple java filename.java and others may have different settings set. If you take a look at your extension preferences as well as your preferences for java by searching your settings, there may be an option, such as "Java Source Paths" enabled which changed where compiled files are saved by VSCode.
If your project is a folder containing Java source files. And no build tools(Maven/Gradle) are used.
If that is the case, you can trigger the command Java: Configure Classpath, then find the section output and then set a relative path to your workspace.
The output files are by default stored inside the workspace storage.
If after triggering the command, what you see is the native vs code setting page. Then try to search java.project.outputPath and then set a relative path to it.

java.lang.IllegalStateException trying to generate unit tests with randoop

My objective is to generate very basic unit tests for a lot of legacy code we are migrating to a new platform. (I know unit tests are not useful this way, but sometimes it happens). We are only blocked for covering percentage so it is good to go with basic tests in the methods we need, so going with an automatically generated tests will be the most efficient strategy.
I found RANDOOP https://randoop.github.io/randoop/ and start trying to make it work. However I found a problem, which in part is due to my almost zero knowledge of java ( I did something similar in .net with just a few clicks and in less than 4 hours).
I´m following official manual here https://randoop.github.io/randoop/manual/index.html#getting_randoop and the video of this guy who just makes it work https://www.youtube.com/watch?v=nPdb-72-EJY.
The Problem
Plain words the problem is the following error when I run this command
java -classpath 'C:\randoop-4.2.1\bin;C:\randoop-4.2.1\randoop-all-4.2.1.jar' randoop.main.Main gentests --testclass=ClassName
> Throwable thrown while handling command:
> java.lang.IllegalStateException: Cannot find the Java compiler. Check
> that classpath includes tools.jar java.lang.IllegalStateException:
> Cannot find the Java compiler. Check that classpath includes tools.jar
> at randoop.compile.SequenceCompiler.<init>(SequenceCompiler.java:64)
> at randoop.compile.SequenceCompiler.<init>(SequenceCompiler.java:48)
> at randoop.condition.SpecificationCollection.<init>(SpecificationCollection.java:82)
> at randoop.condition.SpecificationCollection.create(SpecificationCollection.java:102)
> at randoop.main.GenTests.handle(GenTests.java:279)
> at randoop.main.Main.nonStaticMain(Main.java:66)
> at randoop.main.Main.main(Main.java:30)
>
> Randoop failed. No sequences generated.
what is this above?
I run the command from console placed at the bin folder of randoop "installation" folder.
C:\randoop-4.2.1\bin is the folder where I unzipped Randoop download. Some weird thing is that none of the Randoop version downloads contains the bin folder, so I created it arbitrarily. I don't know if that is right or wrong, but I just did it.
At the beginning the video runs the following command, which is really basic and it worked ok on my system.
java -classpath .\randoop-4.2.1\randoop-all-4.2.1.jar randoop.main.Main help gentests
There is also a text file named myclasslist I don't understand why this guys never talk about. I don't have it.
I have Java 8 installed at c:\program files\jdk1.8.0_231and the Paths and environment variables are set like this.
EDIT
In the video, the guy has the .java file in the randoop root folder. I don´t since I have a real project in intellij. i just found the classs file and copied it to bin folder.
Your screenshot doesn't show System PATH environment variable.
Double check the actual PATH. It may point to JRE instead of the JDK and the System PATH has priority over the User PATH.
Randoop requires JDK to work, not JRE.
Try running:
"c:\program files\jdk1.8.0_231\bin\java.exe" -classpath 'C:\randoop-4.2.1\bin;C:\randoop-4.2.1\randoop-all-4.2.1.jar' randoop.main.Main gentests --testclass=ClassName
If it works, the issue is that default java.exe in your PATH is from JRE not from JDK.
Note that -classpath argument points to the jars or the directories with .class files, not to the individual .class files. See the documentation.
Above command should work if your ClassName.class file is in C:\randoop-4.2.1\bin.
See the related answer for JDK PATH configuration.

Java 9 javapacker can't find jfxrt.jar while creating bss'. Seems totally broken in "createbss" mode under Linux. How to avoid the bug?

I have Ubuntu Linux system with Sun JDE 9.0.1 and I need to convert some of Fx's CSS files to binary form from console.
But when I execute the command:
javapackager -createbss -outdir . -srcdir .
I got an error:
Error: jfxrt.jar needs to be on classpath for -createbss and for
-createJar without -nocss2bin
My JDK is properly installed. I have all necessary modules (including FX).
Any ideas how I can avoid a stupid bug and I make the packager to do it's job?
If I run javapackager from "/usr/lib/jvm/java-9-oracle/jmods" directory it completes without error. So right now I am using an ugly fix, search the modules directory, make it current, run packager from there. But that's damn ugly.
javapackager doesn't accept any other parameters but outdir, srcdir and srcfiles so I can't force loading or looking path for the jfx module. But may there is some trick with environment variables or something that can make the fix less ugly?
P.S. Running from jmod directory or removing "-srcdir" from the parameters eliminates the error, but the packager doesn't generate any output files (even if I specify input files with "-srcfiles"). Seems as javapackager is completely broken in createbss mode. Does anyone use it with Java 9?
I've checked the sources - javapackager from JDK 9.0.1 is totally broken! Use executable file from Java 8. I filled out a bug report to Oracle and all of a sudden they were very surprised (nobody noticed that one of the basic function ot a core tool is broken yet?!).
TEMPORARY SOLUTION: Use the javapackaged tool from JDK 1.8. All you need is the executable itself and you can put at any place. I put it right into my project tree (under "tools" directory) and call from a building script. WARNING! Use this replaced executable for "createbss" ONLY! Use the regular one for all other things!

Building JUNG library with Maven

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.

Running Jar on Snow Leopard with classpath

I'm running snow leopard and I just wrote a Java class on eclipse. The eclipse project references a user library which itself points to a bunch of jar files I've got somewhere in the system. When I run the app through eclipse, everything goes smoothly.
Then I export the class as a jar file and try to run it form the terminal by typing:
java - jar myApp.jar
It throws a java.lang.NoClassDefFoundError exception, meaning that it can't find the libraries I try to reference.
Knowing that my user library jar files are in /Users/myname/tempJars, I also tried to either mention the classpath using the -cp option
(java -cp /Users/myname/tempJars -jar myApp.jar) or to directly reference it in the manifest file. Both attempts failed and the error is the same.
These libraries are Java 1.5 libraries, so I thought I should try and reference another java version by mean of the JAVA_HOME environment variable. I built the following script:
export JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/1.5/Home
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=$CLASSPATH:/Users/myname/tempJars
java -jar myApp.jar
Again, no good. I googled how to execute jars in snow leopard, found the 32/64 bit big deal in some places and even tried executing with the -d32 option, but still to no avail.
Since the same code executes just fine in eclipse, I'm pretty convinced it's just a matter of setting up the JVM so that it includes the user libraries correctly.
Could anyone help me with this?
Thanks in advance.
You need to list the JARs themselves out on the classpath, not just the directory containing them. (Listing the directory is for when you have unarchived .class files in the package hierarchy lying around)
You will probably need to do -cp /Users/myname/tempJars/libA.jar:/.../libB.jar
I believe some (but potentially not all) JVMs support wildcards so -cp /Users/myname/tempJars/* or some variant thereof may work.

Categories

Resources