Compiling and running JUnit in command line - java

I have a project done in Java and we cannot use the IDE to compile it and run it, we are only allowed to use the command line. This project uses JSON. I have managed to successfully compiled and run it as follows:
Compile: javac -cp ./lib/java-json.jar ./src/project/*.java
Run: java -cp ./bin:./lib/java-json.jar project.Main
Now my next step is to run a JUnit test in the command line. To compile I tried:
javac [source files] -cp "path-to-JUnit-jar-files" path-to-JUnit-class
which looks like this:
javac ./src/project/.java -cp "./lib/JUnit/.jar" ./src/testing/AllTests.java
This is the closest I have gotten to successfully compiling the unit test class (I think) because it gave me only a few errors in comparison to other commands I have tried. The errors it gave me are that it needs the classpath to the JSON jar file, but I don't know how to tell the command line to do that.

Related

Javac Compiles Program, but Can't Run with Java

I've never had issues with compiling with javac and running with java before, but here we are.
The program, MainApp.java, depends on a jar file and a few other programs, so I ran the following (which ran without issue):
javac -cp gson.jar *.java
However, when it comes time to run it, I can't seem to get it to work. I've tried:
java -cp gson.jar MainApp.java
but it complains that it can't find the other java files.
I then tried listing the file it said it was missing:
java -cp gson.jar RepositoryData.java MainApp.java
but it gave the same error.
I then tried listing all the dependencies:
java -cp gson.jar Comment.java Issue.java Owner.java Repository.java RepositoryData.java MainApp.java
but it complains that it can't find a main function in the Comment.java file - this leads me to think I'm going about this the wrong way, and I probably shouldn't be directly referencing dependencies in the java call.
I also know that the program does work, as I've compiled it using an editor without issue, but I'm required to do this via the command line for a class.
How should I be properly calling it?

Can't run org.antlr.v4.Tool from terminal inside Intellij Idea

I am trying to use ANTLR in my project, I have Makefile that contains such lines:
JAVAC=javac
JAVAC_FLAGS=-sourcepath .
JAVA=java
JAVA_FLAGS=
PARSER=${JAVA} ${JAVA_FLAGS} org.antlr.v4.Tool
PARSER_FLAGS=-lib Syntax/normal -package Syntax.normal
LEXER=${JAVA} ${JAVA_FLAGS} org.antlr.v4.Tool
LEXER_FLAGS=-lib Syntax/normal -package Syntax.normal
all : test
Syntax/normal/normalLexer.java : Syntax/normal/normalLexer.g4
${LEXER} ${LEXER_FLAGS} Syntax/normal/normalLexer.g4
The last line converts to the:
java org.antlr.v4.Tool -lib Syntax/normal -package Syntax.normal Syntax/normal/normalLexer.g4
When I run Makefile from the Intellij Idea then this java call gives error:
Error: Could not find or load main class org.antlr.v4.Tool
Caused by: java.lang.ClassNotFoundException: org.antlr.v4.Tool
But in the same time, when I use default terminal to run this command, it executes perfectly:
Also if I use default system terminal to run make command, it runs without errors. But inside Inteliji Idea's terminal it gives me error.
The main question is: why terminal inside Intellij Idea can not run the same command as default system terminal?

groovy NoClassDefFoundError

I am very new to groovy and I am trying out this example from the Groovy in Action book. I have this fibonacci.groovy program and when trying to run the program with java command, I am getting the NoClassDefFound error.
The command I am using in the console is:
java -cp %GROOVY_HOME%/embeddable/groovy-all-2.2.0.jar;classes fibonacci
As you can see, I have mentioned the groovy-all jar in the classpath and I set the GROOVY_HOME variable. The classpath variable is not set, so I am assuming it has the default '.' value to find in the current folder itself. What am I doing wrong?
Aren't you missing the current folder in the classpath?
I'm on Linux, but if i compile a Groovy class with groovyc and then try to run it with java, i need to tell java where is my groovy-all.jar and also add the current dir to the classpath
So, this compilation works:
$ groovyc Fib.groovy
But this run doesn't runs:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:classes Fib
As it's missing the current dir in the path:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. Fib
test for fib
Also note that if fibonacci is in a package, you need to type the full path to the class. So for this groovy source:
package up.foo
println "test for fib"
Compile:
$ groovyc Fib.groovy
We write the full package path to execute:
$ java -cp $GROOVY_HOME/embeddable/groovy-all-2.2.0.jar:. up.foo.Fib
There it is:
test for fib

Running JUnit 4 from command line on Eclipse project

I currently have ClasspathSuite set up to run all of my JUnit tests. I'm working on trying to get the ClasspathSuite class to run from the command line. When I am in the bin directory and run this command:
java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore MySuite.class
I get:
JUnit version 4.10
Could not find class: MySuite.class
Time: 0.002
OK (0 tests)
I also tried running the same command with the absolute path to the file, resulting in the same error message. What am I doing wrong?
Just take off the .class part. Java knows to look in that file when you specify the class name.
java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore MySuite

JUnit run tests command line

I've got the following structure
lib/junit-4.10.jar
tests/Tester.java
tests/Tester.class
build/jar/jar_file.jar
(Tester belongs to package tests)
I can compile tests using
javac -cp build/jar/jar_file.jar:lib/junit-4.10.jar tests/*.java
However I can't seem to run tests:
java -cp build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester
or
java -cp build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore Tester
And I get the following output:
JUnit version 4.10
Could not find class: tests.Tester
Time: 0.001
OK (0 tests)
How do I resolve this Could not find class problem? I think it may be classpath related.
Assuming this is Linux/Mac (not Windows) and your path separator is correct (:), since your test class files exist in package subdirectories under the current working directory (.)
You need to add "." to your class path, for example:
java -cp .:build/jar/jar_file.jar:lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester
The classpath should be semi colon separated (on Windows - not sure what you are using.)
java -cp build/jar/jar_file.jar;lib/junit-4.10.jar org.junit.runner.JUnitCore tests.Tester
Also with this command line you will need to run it in your project root

Categories

Resources