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.
Related
Can anyone please post detailed, step-by-step instructions how to install Saxon (10 HE) and how to run a transformation from the command line in MacOS (10.13.6)?
I have installed Java on my computer.
I have downloaded the SaxonHE10-6J.zip file from SourceForge.
Based on the recommendation here I have placed the saxon-he-10.6.jar in the myUserName/Library/Java/Extensions folder.
I then opened the Terminal application and entered a command based on this answer:
java -jar saxon-he-10.6.jar -'/Users/myUserName/Documents/path/to/mystylesheet.xsl' -s:'/Users/myUserName/Documents/path/to/some.xml'
This results in:
Unable to access jarfile saxon-he-10.6.jar
All my attempts, including moving the .jar file to the /Library/Java/Extensions directory or including a full path to the .jar file within the command failed with the same error message.
Please note that I am not a Java developer and I do not intend to use this in an application. All I want is to be able to perform an occasional transformation.
I should also note that the locations of my XML and XSLT files may change from one case to another. I would like to be able to keep the .jar file in a constant location and supply the paths to the XML and XSLT files as required - IOW, I want to have a command template where I only need to change the 2 filepaths (and possibly add some options to the transformation).
What do I need to do?
Eventually I got it working. Although these are not exactly the detailed, step-by-step instructions I was hoping for, I will summarize here what I have learned so far. Hopefully this will save someone the hours of frustration I had to go through.
Make sure you have Java installed on your computer. If not, download
from Oracle and install.
Download Saxon from SourceForge. Unzip and place it anywhere on your
hard disk, except:
do not place it in /Library/Java/Extensions or in myUserName/Library/Java/Extensions;
do not place it within a folder whose name contains a / (see below for description).
To initiate a transformation, make your command:
java -jar 'path/to/saxon-he-10.6.jar' -xsl:'path/to/mystylesheet.xsl' -s:'path/to/some.xml'
Alternatively, you can use:
java -cp 'path/to/saxon-he-10.6.jar' net.sf.saxon.Transform -xsl:'path/to/mystylesheet.xsl' -s:'path/to/some.xml'
This form can be also used to run XQuery by changing
net.sf.saxon.Transform to net.sf.saxon.Query.
For adding more options and/or parameters to your command, see the
instructions given here:
https://www.saxonica.com/html/documentation10/using-xsl/commandline/
but do not follow the instructions at the top of the page regarding
the form of the basic command.
Corrections/additions are most welcome.
Description of the problem with folder name containing /:
Put all 3 files (saxon-he-10.6.jar, mystylesheet.xsl and
some.xml in a folder named XML/RSS in my Documents folder;
Ran the following command:
java -jar '/Users/myUserName/Documents/XML:RSS/saxon-he-10.6.jar' -xsl:'/Users/myUserName/Documents/XML:RSS/mystylesheet.xsl' -s:'/Users/myUserName/Documents/XML:RSS/some.xml'
Received the following error:
Error: Could not find or load main class net.sf.saxon.Transform Caused by: java.lang.ClassNotFoundException: net.sf.saxon.Transform
Moved only the .jar file to the parent folder and ran the following
command:
java -jar '/Users/myUserName/Documents/saxon-he-10.6.jar' -xsl:'/Users/myUserName/Documents/XML:RSS/mystylesheet.xsl' -s:'/Users/myUserName/Documents/XML:RSS/some.xml'
Result: successful transformation.
Moved the .jar file back, renamed the folder to XMLRSS and ran the
following command:
java -jar '/Users/myUserName/Documents/XMLRSS/saxon-he-10.6.jar' -xsl:'/Users/myUserName/Documents/XMLRSS/mystylesheet.xsl' -s:'/Users/myUserName/Documents/XMLRSS/some.xml'
Result: successful transformation.
I don't ever use it myself: however myUserName/Library/Java/Extensions is special as far as the classpath is concerned (you don't need to put JAR files in this directory on the classpath), but it's not special as far as the -jar option is concerned - that needs to be an absolute or relative filename in the normal way and has nothing to do with the classpath.
If you've chosen to put the JAR file in this magic location, then I would use the command java net.sf.saxon.Transform options to pick Saxon up from the classpath rather than identifying the -jar location directly.
There are good reasons for NOT putting Saxon in this magic location, however; one reason is it will affect applications that don't actually want to use Saxon (they might be written to use some other XSLT processor, and you might not actually be aware that they use XSLT at all, until they stop working).
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
This has really got me baffled.
In my Eclipse workspace I have a project called "Java scratchpad". In "Package Explorer" you see "src" (Source Folder) under this and then "root" (Package). Under "root" is a .java file called "LoggingTest.java".
So the path to this java file is "G:\My Documents\software projects\workspace\Java scratchpad\src\root\LoggingTest.java".
When I run the following code in Eclipse:
Path currentRelativePath = Paths.get("");
String s_currRelPath = currentRelativePath.toAbsolutePath().toString();
String pathWithForwardSlashes = s_currRelPath.replace( "\\", "/" );
System.out.println( "path " + pathWithForwardSlashes );
The result is
path G:/My Documents/software projects/workspace/Java scratchpad
But... when I am running the same piece of code at the Command Prompt I have to start in the following directory:
G:\My Documents\software projects\workspace\Java scratchpad\bin
... and then go > java root.LoggingTest at the prompt.
The output from the above bit of code is then:
path G:/My Documents/software projects/workspace/Java scratchpad/bin
In other words, when you run the thing at the Command Prompt you are running a .class file under the \bin directory, but when you run in Eclipse, the Eclipse framework "pretends" that the bin directory doesn't exist.
And the upshot is that I get different values for the "current relative path" (CWD) depending on whether I'm running in Eclipse or at the Command Prompt. If I want to make or use a directory relative to the CWD I'm going to get different values depending on whether I'm running in Eclipse or at the Command Prompt.
I'm feeling quite slow tonight. What should I do? Is there a way of detecting whether a project is being run in Eclipse or at the Command Prompt? Or should I simply try to detect whether the path ends in "\bin" and remove those four characters to make the paths equivalent?
later
Just adding a note in reply to the comment from E-Riz:
It's a simple thing: I want to create logs and I want to create Lucene indices... and to keep things in one location (at this stage anyway) I just want to put this output in a location such as ...\Java scratchpad\output\indices\[name of index] or, respectively, ...\Java scratchpad\output\logging\[name of log].
And, yes, of course at present I do know the absolute path involved here... however, as the name suggests, this is a "scratchpad" or test area... so then this code which uses the CWD to determine where to put or find logs or indices can be used more generically, without having to know the absolute paths involved.
But it has to work either in Eclipse or at the command prompt...!
NB my current workaround is indeed to check whether pathWithForwardSlashes ends in "\bin", and if so to delete these last 4 chars. I can't be the only person to have encountered this oddity. Bet there are cleverer solutions!
There's a couple of facts that you should know or keep in mind:
At runtime, Java can (should) be given a classpath to work with; the default application classpath is just . (the current directory where java was invoked from). That's why it seems to you that you must run your program from the \bin directory of your project; because you haven't told the JVM anywhere else to look for classes. You should be setting the runtime classpath when running your application, which can be done a variety of ways but for simple programs is often done via a batch file or shell script. Note that using -cp to specify the classpath will be crucial when you depend on any JARs.
In Eclipse, since applications aren't run via a command-line, Eclipse has Launch Configurations that encapsulate all the details necessary for launching a program (in "run" or "debug" mode). Here's a decent tutorial (although a bit dated; you can find lots more info about Launch Configurations out there on the Interwebs. The key piece of configuration you're looking for is on the Arguments tab, where you can specify the working directory for a program launch config. The default is the project's root directory. See how that's different than when you've been running your on command line? That explains the difference you're seeing in output. You could change the working directory in the Eclipse Launch Configuration, but I tend to prefer to keep it as the project root.
So, you have a couple of options to make things consistent: use a script/batch to run your app on the command-line, specifying -cp so the JVM knows where your class files are (and any other JARs it might need down the road, too); or, reconfigure your Eclipse launch to match where you run from on the command line. I think the (by far) preferable option is the first one.
Having said all that, you should usually not need to do any path manipulation at all when it comes to files/resources in Java. Everything is relative to either the current working directory where Java was run from, or the classpath.
I have been doing a coding exercise inside the IntelliJ IDEA Community Edition 14 IDE, using OpenJDK.
The project is split over 4 .java files all in the same package.
My end goal is to run this in the terminal/bash (I use System.console().readLine() which doesnt play nicely in the IDE's console).
I've tried navigating to the directory where these 4 files reside (they all reside in the same dir) and tried:
javac -classpath . BibliotecaApp.java Book.java BookManager.java LibraryDB.java
This creates 4 corresponding .class files fine.
The Main is in bibliotecaApp.java/class, so I try run it by:
java BibliotecaApp
But I get the error
Exception in thread "main" java.lang.NoClassDefFoundError: BibliotecaApp (wrong name: com/twu/biblioteca/BibliotecaApp)
Plus about 13 lines of specifics.
Now googling this error seems to be a class path problem, and this is where I get stuck.
From places I've read, usingecho $PATH gives me:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
This is still from the directory of the .java files above.
That path means nothing to me. I have no idea what it is or what it does, or what even a classpath is! Theres alot of resources out there on setting a classpath, but they aren’t helping me because I don't know what it's meant for.
That was a dead end for me. I tried to create a .jar instead using IDEA's Build Artifacts as seen HERE. It created a .jar file but when I navigate to that directory and try run it via:
java -jar biblioteca_jar
I get
Error: Invalid or corrupt jarfile biblioteca_jar
Another issue is that in the file explorer, the file actually comes out as biblioteca.jar, but ls on that dir shows biblioteca_jar. Is that normal?
The code is on my GitHub if that helps anything
https://github.com/botagar/twu-biblioteca-JohnGeddes
Based on your compiler step, change this
java BibliotecaApp
to
java -cp . BibliotecaApp
Which will add the current directory to the classpath for the java runtime environment. See also the Oracle Technote Setting the Class Path.
A jar file is a kind of zip, and should have a .jar extension. So this
java -jar biblioteca_jar
should probably be
java -jar biblioteca.jar
And you can check if the file is valid with any zip archive reader. Including jar itself,
jar tvvf biblioteca.jar
Edit
Based on your comments below,
cd ~/Documents/ThoughtWorks Uni/TWU_Biblioteca-master/src/
and then
java -cp . com.twu.biblioteca.BibliotecaApp
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.