Java compilation / package / namespace error - java

This is the stupidest, simplest problem ever with basic Java, but what am I doing wrong?
In a directory structure called
com/myname/robos
- Robo.java
- Arena.java
Robo.java :
package com.myname.robos;
public class Robo {
public void fala() {
System.out.println("Gleep Beep Boop!");
}
}
Arena.java :
package com.myname.robos;
import com.myname.robos.Robo;
public class Arena {
public static void main(String[] args) {
Robo r2 = new Robo();
r2.fala();
}
}
When I try to :
javac Robo.java
it compiles.
When I then try to
javac Arena.java
I get
Arena.java:3: error: cannot find symbol
import com.myname.robos.Robo;
^
symbol: class Robo
location: package com.myname.robos
Arena.java:8: error: cannot find symbol
Robo r2 = new Robo();
^
symbol: class Robo
location: class Arena
Arena.java:8: error: cannot find symbol
Robo r2 = new Robo();
^
symbol: class Robo
location: class Arena
3 errors
I KNOW it's an error about incompatibility between directory / package etc. names.
But I still never get this right. What should I be writing?

You need to ensure that your files are in the same structure as your package reference, say:
if your .java files are for instance at ../Desktop you must create folders with names: com, myname and robos and then paste them there, so your files will be inside ../Desktop/com/myname/robos.
Then you just need to compile Arena.java and run it:
$ javac com/myname/robos/Arena.java
$ java com/myname/robos/Arena
Positioning you inside ../Desktop

Related

How can I execute Taurus junit?

I made a taurus test spesifcation:
execution:
- executor: junit
iterations: 5 # loop over test suite for 5 times
concurrency: 20 # number of virtual users
ramp-up: 1m # time of load growing
steps: 5 # number of steps of growing
scenario:
script: src/test
modules:
junit:
junit-version: 5
working-dir: src/main/java
My Unit Test are:
package org.steinko.springtutorial;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.steinko.springtutorial.Main;
public class MainTest {
#Test
void shouldReturnANumber(){
Main main = new Main();
String[] arg = new String[1];
Main.main(arg);
int number = main.getNumber();
assertTrue(0 < number);
assertTrue(number < 100);
}
}
My source code place is:
package org.steinko.springtutorial;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.steinko.springtutorial.NumberGenerator;
public class Main {
private static final Logger log = LoggerFactory.getLogger(Main.class);
private static int number;
private static final String CONFIG_LOCATION = "beans.xml";
public static void main(String[] args )
{
log.info("Guess the number game");
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(CONFIG_LOCATION);
NumberGenerator numberGenerator = context.getBean("numberGenerator", NumberGenerator.class);
number = numberGenerator.next();
log.info("number = {}", number);
context.close();
}
public int getNumber() {
log.info("getNumber",number);
return number;
}
}
When I run bzt ./performanctests/unittests.yaml
I get a error:
[2019-03-17 15:34:25,556 ERROR root] Child Process Error: Javac exited
with code: 1
/Users/stein/Development/guess-the-number-game/core/src/test/java/org/steinko/springtutorial/MainTest.java:7:
error: cannot find symbol
import org.steinko.springtutorial.Main;
^
symbol: class Main
location: package org.steinko.springtutorial
/Users/stein/Development/guess-the-number-game/core/src/test/java/org/steinko/springtutorial/MainTest.java:15:
error: cannot find symbol
Main main = new Main();
^
symbol: class Main
location: class MainTest
/Users/stein/Development/guess-the-number-game/core/src/test/java/org/steinko/springtutorial/MainTest.java:15:
error: cannot find symbol
Main main = new Main();
^
symbol: class Main
location: class MainTest
/Users/stein/Development/guess-the-number-game/core/src/test/java/org/steinko/springtutorial/MainTest.java:17:
error: cannot find symbol
Main.main(arg);
^
symbol: variable Main
location: class MainTest
4 errors
How do I fix this error?
This seems like you're trying to use an external dependency in your java class. Taurus doesn't exactly build the module before it tries to compile. So any dependencies that your project has in the classpath would not be recognized. The solution to this is to upload the dependencies as .jar files along with the .java file in the BZ test. Basically, any class/interface that you import in your test script should be supplied as a jar. So your config file would look something like this:
- executor: junit
iterations: 5 # loop over test suite for 5 times
concurrency: 20 # number of virtual users
ramp-up: 1m # time of load growing
steps: 5 # number of steps of growing
scenario:
script: src/test
additional-classpath: # just an example - you can include your own .jar files
- rest-assured-2.9.0.jar
- log4j-1.2.17.jar
- groovy-2.4.4.jar
- rest-assured-common-2.9.0.jar
- json-path-2.9.0.jar
modules:
junit:
junit-version: 5
working-dir: src/main/java
More details at https://gettaurus.org/docs/JUnit/

Under one package : compiler fails to read other class

I have three java files in one package : 'Receiver'.
CMReceiverMutant.java
CMReceiverMutantContext.java
TestDriver.java
Here is my TestDriver.java
package Receiver;
public class TestDriver{
public static void main (String[] args){
TestCase1();
// alternateTestCase1();
}
public static void TestCase1(){
CMReceiverMutant obj = new CMReceiverMutant();
obj.INT1SurvFlag();
obj.Exitw0();
System.out.println("Test case 1 reaches state :"+obj._fsm.getState().getName());
if(obj._fsm.getState().getName().equals("CMReceiverMap.Final"))
System.out.println("Test Case 1 passes!");
else
System.out.println("Test Case 1 fails");
}
}
I compiled TestDriver which depends on CMReceiverMutant.java. Eventhough I put them in the same directory. The compiler seems can't read CMReceiverMutant.java and it makes error :
TestDriver.java:11: error: cannot find symbol
CMReceiverMutant obj = new CMReceiverMutant();
^
symbol: class CMReceiverMutant
location: class TestDriver
I use cmd
javac -classpath Receiver\TestDriver.java
and I've tried
javac -classpath Receiver*.java
The errors are the same. Can you tell me whats the problem is?
Thank you
please check "Source" packages in the "Java Build Path" sometimes if the package isn't registered there the compiler fails to load them.
Hopefully it worked for me.

IntelliJ and Vertx: How to run org.vertx.java.deploy.impl.cli.Starter ?

I have the following code inside Maven project:
package hello;
import org.vertx.java.core.Handler;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.deploy.Verticle;
public class Server extends Verticle {
public void start() {
vertx.createHttpServer().requestHandler(new Handler<HttpServerRequest>() {
#Override
public void handle(HttpServerRequest req) {
System.out.println("Got request: " + req.uri);
System.out.println("Headers are: ");
for (String key : req.headers().keySet()) {
System.out.println(key + ":" + req.headers().get(key));
}
req.response.headers().put("Content-Type", "text/html; charset-UTF-8");
req.response.end("<html><body><h1>Hello from vert.x!</h1></body></html>");
}
}).listen(4000);
}
}
I also have the following dependency in pom.xml:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.vert-x</groupId>
<artifactId>vertx-platform</artifactId>
<version>1.2.3.final</version>
</dependency>
I try to run Java application in IntelliJ with the following configuration:
and I get many errors:
Error:(7, 8) java: hello.Server is not abstract and does not override
abstract method stop(io.vertx.core.Future) in
io.vertx.core.Verticle Error:(12, 57) java: cannot find symbol
symbol: variable uri location: variable req of type
io.vertx.core.http.HttpServerRequest Error:(14, 48) java: cannot find
symbol symbol: method keySet() location: interface
io.vertx.core.MultiMap Error:(17, 20) java: cannot find symbol
symbol: variable response location: variable req of type
io.vertx.core.http.HttpServerRequest Error:(18, 20) java: cannot find
symbol symbol: variable response location: variable req of type
io.vertx.core.http.HttpServerRequest Error:(9, 9) java: cannot find
symbol symbol: variable vertx location: class hello.Server
However, when I download vert.x jar files from here:
http://vertx.io/download/
and put them in the project structure, then the same code compiles successfully.
Probably I need another dependency in pom.xml, but I don't know what it should be.
You shouldn't have the dependency:
<dependency>
<groupId>org.vert-x</groupId>
<artifactId>vertx-platform</artifactId>
<version>1.2.3.final</version>
</dependency>
Since it related to Vert.x 1.2 and your application in on 3.x. In this case your main class should be:
io.vertx.core.Launcher
Alternatively you could add a main method and debug from there, for example:
public class Server extends Verticle {
public static void main(String[] args) {
Vertx.vertx().deployVerticle(new Server());
}
}
Now you don't need to worry about adding launcher profiles, just right click and run/debug.

classes in same package not see each others and not compiled in terminal

package mainPackage
public class MainClass{
Public static void main(String[] args){
SwingUtilities.invoikeLater(new Runnable()){
Login login = new Login();
}
}
}
both classes ( MainClass & Login ) at the same package (mainPackage).
On netbeans IDE the application works fine (compile & execute ).
But when using Terminal to compile MainClass I get this error :
error: cannot find symbol
Login login= new Login();
^
symbol: class Login
here is my directory :
/home/momy/application
|
src
|
mainPackage
|
MainClass.java
Login.java
To compile code:
application$ javac src/mainPackage/MainClass.java
any idea why?
application$ javac src/mainPackage/MainClass.java
The problem is here. You should be in the src directory:
application/src$ javac mainPackage/MainClass.java

Java: multiple packages minimum working example

Trying to understand how Java packages work with classpath etc. In ~/java/tmp/test/HelloWorld I created HelloWorld.java:
package test;
import test2.Hello2;
public class HelloWorld {
public static void main(String[] args) {
Hello2 x = new Hello2();
x.blagh(args);
}
}
Then in ~/java/tmp/test2/Hello2 I created Hello2.java:
package test2;
public class Hello2
{
public static void blagh(String[] args) {
System.out.println("Hello, World");
}
}
Working in ~/java/tmp, I try to compile using:
javac -g test/HelloWorld/HelloWorld.java
I get the following errors:
test/HelloWorld/HelloWorld.java:3: package test2 does not exist
import test2.Hello2;
^
test/HelloWorld/HelloWorld.java:8: cannot find symbol
symbol : class Hello2
location: class test.HelloWorld
Hello2 x = new Hello2();
^
test/HelloWorld/HelloWorld.java:8: cannot find symbol
symbol : class Hello2
location: class test.HelloWorld
Hello2 x = new Hello2();
I've tried things like setting the classpath (to every possible combination of the above paths I could think of), changing which directory I run the compiler from, etc. Nothing works. Please help.
Your HelloWorld.java belongs to package test, so it should live in a directory named test, not test/HelloWorld. Same with Hello2.java, it should live in test2, not test2/Hello2. Move HelloWorld.java to the test directory and Hello2.java to the test2 directory and give it another try.

Categories