I'm just starting to learn Java.
I am using IntelliJ to create a SparkJava (see: sparkjava.com) app using openjdk 17. I followed the tutorial instructions outlined in "Setting up Spark with Maven" (https://sparkjava.com/tutorials/maven-setup). I believe the instructions are very outdated because they did not work. After some googling, I finally just arrived at the following code and POM.xml. When I build the project, I get an error: java: package spark does not exist
I don't know what to do.
I added the dependency to my POM.xml.
I added the apache.spark.core_2.13 library via Project Structure.
I googled "IntelliJ package does not exist" but couldn't find a helpful answer. Most everyone said to "add the dependency to the POM" but I've already done that.
I googled "add package to Java project IntelliJ" but after clicking a number of linkis, I couldn't find a helpful answer. I tried a few of the suggestions, but none resolved this problem.
I think I am missing something fundamental here, like somehow I'm not telling IntelliJ where to find the spark code.
src/main/java/Sparky.java
import static spark.Spark.*;
public class Sparky {
public static void main(String[] args){
get("/hello", (req, res) -> "Hello World");
}
}
pom.xml
<?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>sparky</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependencies>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.13</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
I guess you didn't configure IntelliJ for Maven properly. Either your project is not a Maven project (as far as IntelliJ is concerned, even if the structure of the project is valid for Maven), or you're missing an IntelliJ plugin or something. Look at IntelliJ's doc on how to create and manage a Maven project.
The dependency package was completely wrong. It was pointing at Apache Spark, not Spark Java.
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.3</version>
</dependency>
Related
I'm trying to create a maven project in intellij, to create a parser in antlr. Here is my pom.xml:
<?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>com.mua</groupId>
<artifactId>json-parser-java-antlr</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Json parser Java ANTLR</name>
<packaging>jar</packaging>
<description>Trying to create a parser using ANTLR in Java, as facing problems with LLVM</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
</dependency>
</dependencies>
</project>
When I click on import changes, it loads for just 1-2 seconds then done. But if I try to import MapUtils from org.apache.commons.collections4.MapUtils it says it can't resolve common, although I added in <groupId>org.apache.commons</groupId> dependency.
I'm new in maven project creation and management.
So, what is the problem here and how can I resolve this problem ?
I studied some pom.xml and found a parent attribute. No idea how to configure that.
My Eclipse editor shows:
Project build error: 'dependencies.dependency.version' for org.antlr:antlr4-runtime:jar is missing.
Project build error: 'dependencies.dependency.version' for org.apache.commons:commons-collections4:jar is missing.
Project build error: 'dependencies.dependency.version' for org.apache.directory.studio:org.apache.commons.io:jar is missing.
None of the dependencies work, because it doesn't know which version of them you want.
Specifying dependencies without a version is something you do when you have a parent pom. You don't, so versions are mandatory.
You could try adding a specific version of antlr to your pom.xml. And if you are using the antlr plugin as well, make sure the version of antlr run-time you are using is the same as the built-in version of the plugin.
I am new with Maven and Spark and I would like to play a bit with both of them.
I am on OSx so I've installed both using brew.
On eclipse I created a Maven project with the quick wizard and created 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>com.and.app</groupId>
<artifactId>myapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.4.3</version>
</dependency>
</dependencies>
</project>
while the main class is:
package myapp;
import static spark.Spark.*;
public class Driver {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World"); }
}
But I get the following error:
Missing artifact com.sparkjava:spark-core:jar:2.4.3
I thought it was because of the brew installation and so I've add the line:
<systemPath>\usr\local\Cellar\apache-spark\2.4.3\libexec\jars</systemPath>
as an explicit path for spark jars just after the version tag in the pom.xml
Until now I have no success.
What am I doing wrong?
P.S.: I verified Maven and Spark installation.
Maven is getting the dependencies automatically from the network. If you want it to use locally installed jars, you have to add them to your local maven repository:
https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html
But I am wondering: You could also tell maven to get the spark dependency itself. It seems to be available for maven users:
https://mvnrepository.com/artifact/org.apache.spark/spark-core
So the dependency should be:
<!-- https://mvnrepository.com/artifact/org.apache.spark/spark-core -->
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_2.12</artifactId>
<version>2.4.3</version>
</dependency>
Check the definition of the system scope:
This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.
It means that you should provide the path to jar for each launch of your code. And for compilation as well (because of import). Change the scope or provide the path to the jar for compiler explicitly.
How do you set up JMonkeyEngine in Intellij IDEA. It was not specified in the documentation (http://hub.jmonkeyengine.org/wiki/doku.php/).
now it's easy if you use maven or gradle.
Maven simple pom.xml:
<?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>com.mycompany</groupId>
<artifactId>jme3-example</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>JME3 maven project</name>
<properties>
<jme3.version>3.0.10</jme3.version>
</properties>
<dependencies>
<dependency>
<groupId>com.jme3</groupId>
<artifactId>jme3-core</artifactId>
<version>${jme3.version}</version>
</dependency>
<dependency>
<groupId>com.jme3</groupId>
<artifactId>jme3-desktop</artifactId>
<version>${jme3.version}</version>
</dependency>
<dependency>
<groupId>com.jme3</groupId>
<artifactId>jme3-lwjgl</artifactId>
<version>${jme3.version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>jme3-repo</id>
<name>JME3 maven repo</name>
<url>http://updates.jmonkeyengine.org/maven/</url>
</repository>
</repositories>
</project>
Note 0: JME 3.1 is deployed at JCenter
Note 1: The JMonkeyEngine official wiki is moved to https://jmonkeyengine.github.io/wiki/
Gradle config and other maven dependencies documented here: https://jmonkeyengine.github.io/wiki/jme3/maven.html
If you didn't use maven or gradle before, please, learn it :)
I recommend that you learn using the jMonkeyEngine3 with the jMonkeyEngine SDK,
as that is well documented.
If you want to then use jMonkeyEngine with IntelliJ IDEA,
you can get all relevant information from this tutorial for eclipse:
http://hub.jmonkeyengine.org/wiki/doku.php/jme3:setting_up_jme3_in_eclipse
Of course, you must consider different terminology and UI for setting up things in IDEA.
Basically all you have to do is add the correct .jar files to your classpath.
If you have problems and need more information,
you should probably read more about IntelliJ IDEA Project settings and Java Classpath.
As I said, I recommend "starting" with the jMonkeyEngine SDK.
(Keep in mind that you lose support for jMonkey file types, such as .j3m (jmonkey materials) and .j3o (jmonkey models) when you use IntelliJ IDEA!)
I need some help with setting up a spring project.
I am busy going through the book “Spring in action” and I need to try some of the examples out. I have looked at plenty of pages and nowhere can is see where I am going wrong. It must be something silly that I missed or overlooked.
I installed Spring source tool suite
Created a new java/maven project
Added a new applicationContext.xml bean definition file
The outline of the project looks as follows
I created my beans (vwCar & nissanCar that implements car interface) and where it comes to use them I have a main method in the app class. I need to create an application context.
ApplicationContext context = new ClassPathApplicationContext("src/main/resources/applicationContext.xml");
But I have difficulty creating the ApplicationContext. It gives me an error and code assist don’t work
Using code assist the only thing that it suggests is (Pressing Ctrl+Space after typing app):
If I just type it out I get an error
ApplicationContext cannot be resolved to a type in class App.java
Is there something that I should import myself?
I can see an "S" on the project folder - doesn't this indicate that the project is already spring enabled?
---------ADDED AFTER ALEX COMMENTED TO SUGGEST THAT I SHOULD ADD A MAVEN DEPENDENCY-------------
I added the missing dependency like Alex suggested but I don't know what the correct version is. If I look a the STS directory I see several files named ... 2.9.2
org.springframework.ide.eclipse.feature_2.9.2.201205070117-RELEASE
but if I add the dependency with 2.9.2 i get the following error on my POM
Missing artifact org.springframework:spring-context:jar:2.9.2
My POM looks like below
<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>my.chrispie</groupId>
<artifactId>MyMavenSpringProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyMavenSpringProject</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>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
</project>
Since you're using Maven you should add the following dependency to your pom.xml:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
</dependency>
Where ${org.springframework-version} should be replaced with the version you are using.
This will ensure that the Spring jars needed to get started are available to your application.
It must be something silly that I missed or overlooked.
I think you'v just overlooked the dependency management functionality of Maven. Creating a Java/Maven project doesn't pull in the required Spring jars. If you used a template project from the STS landing page this would all have been setup for.
[CLOSED - I had to move classes from test/java to main/java and update the maven repository via the IDE "maven options"]
I'm new to maven and inexperienced with java development. I'm using IntelliJ Idea as IDE. I'm using Maven 3.0.4.
I created a "project A" and a "project B", each with some classes. Now when I try to create a dependency in project A to a class in project B I can't seem to find any classes that are part of project B. When I check the maven repository I can see that a .jar file is created based on project B.
To clarify: when adding a dependency to project B I do find an artifact called "project B" but I cannot find any classes that are part of project B.
It doesn't seem like I can access and use any of the classes that are part of project B inside project A which would make this installation worthless.
--
Please tell me what information I should include for you to help me solve this problem.
[EDIT] Here is the project A's pom with a dependency on project B. However I either don't understand how to use it in my project or it doesn't work. While I can use IntelliJ's functionality to find and add the artifact, intelliJ can't seem to find any classes that are part of project B (while it does find classes that are part of other pre-defined packages in the maven repository):
<?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>planet</groupId>
<artifactId>planet</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>toolbox</groupId>
<artifactId>toolbox</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
I had to move my classes from test/java to main/java. Silly, but it took me a whole day to realise this. This only worked after updating the repository in "maven options" via the IDE.
You can do the following:
<dependency>
<groupId>toolbox</groupId>
<artifactId>toolbox</artifactId>
<version>1.0</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
Works for me :)