sparkjava and eclipse unresolved compilation problems: - java

I have followed the tut here http://sparkjava.com/documentation.html#getting-started
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spark</groupId>
<artifactId>Spark</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spark</name>
<description>spark</description>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</project>
Hello.java
import static spark.Spark.*;
public class Hello {
public static void main(String[] args) {
get("/hello", (req, res)-> "Hello World");//eclipse error this line
}
}
I got an error with eclipse
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
req cannot be resolved to a variable
Syntax error on token ",", . expected
Syntax error on token "-", -- expected
at Hello.main(Hello.java:5)

Java's lambda expressions are a feature in Java 8: Oracle link. You'll need to change your compliance level (and java version, and pom) to java 8 to make this example compile.

Related

Jdivert java.lang.ClassNotFoundException error when ran

Trying to use this library, but whenever I run it. It gives me the following. I updated my pom file and thought it would be the solution, but I still receive an error.
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jna/Pointer
at org.example.Main.main(Main.java:11)
Caused by: java.lang.ClassNotFoundException: com.sun.jna.Pointer
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
Here is my code
public class Main {
public static void main(String[] args) throws WinDivertException {
System.out.println("Hello world!");
WinDivert w = new WinDivert("tcp.DstPort == 80 and tcp.PayloadLength > 0");
w.open(); // packets will be captured from now on
Packet packet = w.recv(); // read a single packet
System.out.println(packet);
w.send(packet); // re-inject the packet into the network stack
w.close(); // stop capturing packets
}
}
And here is my pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Glacial1.1</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.ffalcinelli</groupId>
<artifactId>jdivert</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Tried updating my pom file and it didn't work

Run a jar file from unix command line

I have created a jar file from mvn command but when i am trying to run it i am getting below error :
java -cp target/new-repl.jar package.repl
Error: Could not find or load main class package.repl
Caused by: java.lang.ClassNotFoundException: package.repl
I got many links on stack overflow but none of them helped me. Could you please help me to run this from command line. Below is the my .java file:
package repl;
public class Test {
public static void main(String[] args) throws IOException {
....
}
}
POM.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>repl</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>repl</name>
<description>build tar for repl</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<build>
<finalName>new-repl</finalName>
<directory>target</directory>
</build>
</project>
You can do like this. Type fully qualified class name instead of typing package name
java -cp target/new-repl.jar repl.Test
The error says it all
Error: Could not find or load main class package.repl
your class name is not package.repl, it is repl.Test

Bootstrap a simple Java SE "Hello World" desktop application using JBoss Weld environment causes a runtime error

I'd like to use JBoss Weld as a CDI facility in a classic Hello World application in a maven structured project. To keep the things as clean & simple as possible, I only created a Weld environment object and nothing more. I also created the deployment descriptor file beans.xml in src/main/resources/META-INF and src/test/resources directories. I did't go further by creating and initializing a WeldContainer etc. I'm just wondering why this settings don't work in the first place.
The application compiles perfectly and generates an executable jar file with mvn package command. However I got a runtime error:
C:\dev\eclipse-workspace\my-app>java -cp target/my-app-1.0.jar app.Hello
Exception in thread "main" java.lang.NoClassDefFoundError: org/jboss/weld/environment/se/Weld
at app.Hello.main(Hello.java:7)
Caused by: java.lang.ClassNotFoundException: org.jboss.weld.environment.se.Weld
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.j
ava:602)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoader
s.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
So, here is my main class:
1 package app;
2
3 import org.jboss.weld.environment.se.Weld;
4
5 public class Hello {
6 public static void main(String[] args) {
7 Weld weld = new Weld();
8
9 System.out.println("Hello World");
10
11 weld.shutdown();
12 System.exit(0);
13 }
14 }
my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>jee</groupId>
<artifactId>my-app</artifactId>
<version>1.0</version>
<name>my-app</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>3.1.3.Final</version>
</dependency>
</dependencies>
</project>
and lastly my project structure:
Your help is appreciated in advance.
Thank you.
Alex
You will note that java -cp target/my-app-1.0.jar app.Hello does not include the Weld jars on the classpath (unless your jar file's META-INF/MANIFEST.MF has a Class-Path entry that references them). That is why Weld classes cannot be found at runtime when you start your application in this manner.

"not all expectations were satisfied" missing under Eclipse

We use two different IDEs, Netbeans 8.2 and Eclipse 4.7.2. We are running JMock 2.8.3 with JUnit 4.11 and have a test that fails under Netbeans and Jenkins (using the Netbean's Ant scripts), but passes under Eclipse.
The error is "not all expectations were satisfied".
However, if I add an assertIsSatisfied() call to the end of the test, it will fail with the correct error message under Eclipse.
I can reproduce this with a trivial example:
public class FailureExample {
private static class Example {
public void doSomething() { }
public void doSomethingElse() { }
}
// Mocks
#Rule public JUnitRuleMockery context = new JUnitRuleMockery(){{
setThreadingPolicy(new Synchroniser());
setImposteriser(ClassImposteriser.INSTANCE);
}};
public Example instance;
#Before
public void setUp() throws Exception {
// Mocks
instance = context.mock(Example.class);
}
#Test
public void testExample() {
context.checking(new Expectations() {{
oneOf(instance).doSomething();
oneOf(instance).doSomethingElse();
}});
instance.doSomething();
}
}
Is there something else I need to do in Eclipse to make JMock behave as expected?
Update
Adding screenshot of our project's libraries:
UPDATE
I tried create a new Java project as well as a new Maven project (as described below by Till Brychcy) as those worked. I tried removing all the jar files listed for my project and then readding them, but it failed.
I'm very close to abandoning Eclipse in favor of Netbeans, simply because I have real work to do, not just fighting with Eclipse.
I cannot reproduce your problem.
With both Eclipse 4.7.2 and Eclipse built from the current master I get:
java.lang.AssertionError: not all expectations were satisfied
expectations:
expected once, already invoked 1 time: example.doSomething()
! expected once, never invoked: example.doSomethingElse()
what happened before this:
example.doSomething()
I used the following pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>jmockbug</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jmockbug</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-junit4</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>org.jmock</groupId>
<artifactId>jmock-legacy</artifactId>
<version>2.8.3</version>
</dependency>
</dependencies>

Maven Build Error - try-with-resources is not supported in -source 1.5 [duplicate]

This question already has answers here:
How do you specify the Java compiler version in a pom.xml file?
(5 answers)
Closed 5 years ago.
This is my simple code from a Maven project which has try-with-resources.I use eclipse as my IDE.
public class Hello {
public static void main(String[] args) {
try(FileInputStream fi = new FileInputStream("ANC")){
} catch (IOException e) {
e.printStackTrace();
}
}
}
When I build this using clean package -U it gives me the following error.
Hello.java:[11,20] try-with-resources is not supported in -source 1.5
(use -source 7 or higher to enable try-with-resources).
However,I have my java compiler to be set at Java 1.8 and in Jave Build path JRE System Library is also JDK 1.8.Still this error persists.
This is my POM file ( minus the junit dependency )
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.commonutil</groupId>
<artifactId>PropertyFile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>PropertyFile</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Note : Setting the target and source in pom.xml did not help either.
Any help regarding this is appreciated.Thank you.
You need to specify the java source version, so the correct flag value is used by the compiler plug-in. For example:
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>

Categories

Resources