Using imported module in intellij - java

I'm having trouble figuring out how to actually use an imported module inside of intellij.
I'm trying to make use of maryTTS. More exactly MaryInterface. https://github.com/marytts/marytts/wiki/MaryInterface
Readme says use maven or gradle. I've never used maven, not that that means I can't, but my current project is not a maven project. Just a plain java project. Same with gradle. I'll try maven.
I started just a plain new project called test.
Then I imported the module via:
File->New->Module from existing sources.
Which left me with a module that I could not/did not know how to access. So basically two separate modules in my project.
That meaning if I use this test code:
import javax.sound.sampled.AudioInputStream;
import marytts.LocalMaryInterface;
import marytts.MaryInterface;
import marytts.exceptions.MaryConfigurationException;
import marytts.exceptions.SynthesisException;
import marytts.util.data.audio.AudioPlayer;
public class Voice
{
private MaryInterface marytts;
private AudioPlayer ap;
public Voice(String voiceName)
{
try
{
marytts = new LocalMaryInterface();
marytts.setVoice(voiceName);
ap = new AudioPlayer();
}
catch (MaryConfigurationException ex)
{
ex.printStackTrace();
}
}
public void say(String input)
{
try
{
AudioInputStream audio = marytts.generateAudio(input);
ap.setAudio(audio);
ap.start();
}
catch (SynthesisException ex)
{
System.err.println("Error saying phrase.");
}
}
}
All of the marytts imports fail from my main module. Obviously they are fine in the marytts module.
I also tried creating a blank maven project, then adding the example code to the pom.xml . I changed the artifactId to marytts. It then just gave a path error under dependencies for files in ~/.m2 that were there.
Example here. https://github.com/marytts/marytts
<repositories>
<repository>
<id>central</id>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>de.dfki.mary</groupId>
<artifactId>marytts</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
I've looked through intellj's docs. The module import seems pretty straightforward. Obviously I'm not getting part of the process or doing something wrong.
So my question then is what are the correct steps to be able to call that interface from my main module? Should I use/learn maven?

I'm not sure why this is the answer but it seems to work.
I used the exact verbage from the README but had to ad an id.
<repositories>
<repository>
<id>jcenter</id>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>de.dfki.mary</groupId>
<artifactId>voice-cmu-slt-hsmm</artifactId>
<version>5.2</version>
</dependency>
</dependencies>
This seemed to work in as much as I can use all the import statements and create a MaryInterface.
What I do not understand is why it would not have worked this way. I just assumed I needed the marytts artifact.
http://cs.unk.edu/~mcconvilletl/?p=59

Related

cannot add spigot to build path succesfully for 1.18 plugins

I've been trying to import spigot/bukkit for minecraft plugins. When trying to create the main class, I entered
public class Main extends JavaPlugin{
}
With an error under JavaPlugin, since their is no import. The tutorial im following told me to click the fix that will import it for me, but
the fix simply does not show up when I attempt to resolve it, and if I manually import it, it gives the error: "the import org.bukkit.plugin cannot be resolved." I've tried restarting the project, deleting and reinstalling, and everything in between. Please let me know if you need more information on how I've added spigot to the build path, or anything else I can help with.
Since the 1.17, it seems to have some changes with jar. Now, if you start server, it will create the bundler/versions folder, with a given jar that -for me- should fix your issue.
Also, you can use more preferable way to import project, such as maven or gradle. They can help you to easier share the project, and make it faster to run (you can also automatically run it with github actions for example.
To use spigot with maven, use this:
<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.18.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
(Documentation)
To use spigot with gradle, use this:
repositories {
maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url = 'https://oss.sonatype.org/content/repositories/central' }
}
dependencies {
compileOnly 'org.spigotmc:spigot-api:1.18.2-R0.1-SNAPSHOT'
}
(Documentation)

Exception in thread "main" java.lang.NoClassDefFoundError: scala/Serializable while using ActorSystem in Java project

I am new to java and AKKA toolkit. I have created a JAVA project and tried to include the below code
package com.postgresqltutorial;
import akka.actor.ActorSystem;
public class App {
public static void main(String[] args) {
final ActorSystem system = ActorSystem.create("QuickStart");
}
}
I have used AKKA libs in referenced libs as akka-actor_2.12-2.6.15.jar, akka-protobuf_2.12-2.6.15.jar and akka-stream_2.12-2.6.15.jar.
And my project structure is like
project structure
Please help me to resolve this.
Most likely you've not referenced the libraries correctly. That is why you should use a build tool such as Maven. Check the referenced link to understand how it works. It handles the libraries for you, you just have to add them in the pom.xml file.
Example:
<dependencies>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_3</artifactId>
<version>2.6.18</version>
</dependency>
</dependencies>

Need help to get YouTube Java API code working

I'm trying to make a simple program that calls the API to check video views after finding this example here but have run into a bit of trouble. I found the code needed to initialise a YouTube object here (https://github.com/youtube/api-samples/blob/master/java/src/main/java/com/google/api/services/samples/youtube/cmdline/data/GeolocationSearch.java#L90)
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
#Override
public void initialize(HttpRequest request) throws IOException {
}
}).setApplicationName("youtube-cmdline-geolocationsearch-sample").build();
I noticed that the code requires the class Auth, which is defined in the import
import com.google.api.services.samples.youtube.cmdline.Auth;
This is in an api-samples repo from Google and not included in the YouTube API dependency I have defined in my Maven pom.xml in my Eclipse project. I decided to just copy the Auth.java file and place it in my project's src/main/java folder but now I have another problem: 2 imports in Auth.java cannot be resolved. These are the ones:
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
You can see the entirety of Auth.java here.
In case it'll help here is the dependency definition from my pom.xml file:
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-youtube</artifactId>
<version>v3-rev20201202-1.31.0</version>
</dependency>
</dependencies>
Thank you for taking time to read this, and I hope you can find a solution for me. :)
Thanks to stvar's comment I looked at the pom.xml file from the api-samples repo and found what I was missing was a Google OAuth client thing. I added the following code to my pom.xml and the imports were resolved.
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.20.0</version>
</dependency>

reference my own class in a pom file

I am using "spring MVC" to build something(project “A”). I use maven to get the jar I need.
But I have a question, if I have a Java file(a separate file), a class in it, how can I use it in project “A” just like the other in pom.xml?
maybe it like this: when i use JSONObject i must edit pom.xml like:
<dependency>
<groupId>net.sf.json-lib<groupId>
.........
</dependency>
I mean i want my java file like JSONObject,i put it in pom file when it want use it in some project:
test.java:
class bc {
public String test() {
return "bc->test";
}
}
pom.xml
<dependency>
<groupId>test<groupId>
.........
</dependency>
controller file (in springMVC)
import ...
#controller
#requestMapping(value="/api")
public class apiController {
#RequestMapping.....................
public #ResponseBody String testMyMavenClass() {
return new bc().test()
}
}
Im not sure about how to use .class in maven pom file but Im sure you can define your own jar file.
First, combine your classes into jar file.
Then, Define it inside pom.xml as external library.
Reference: https://www.tutorialspoint.com/maven/maven_external_dependencies.htm
One more note, you can add below line to make sure maven knows where is external repository:
<repositories>
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file:/${project.basedir}/libs</url>
</repository>
</repositories>
Method 1: Install jar file to local maven repository:
Export your project to jar file, then
http://www.mkyong.com/maven/how-to-include-library-manully-into-maven-local-repository/
Method 2: Use a repository manager. This is more advanced and requires some server skills. I can give you the name & link only, and you should do your research your self: https://www.sonatype.com/download-oss-sonatype

Neo4j, REST API, java - cypher queries

I learn REST API with Java and tried run this simple code, but I got error. Something wrong with this part of code: RestAPI graphDb = new RestAPI.... I use this external JAR (http://m2.neo4j.org/content/repositories/releases/org/neo4j/neo4j-rest-graphdb/2.0.0/neo4j-rest-graphdb-2.0.0.jar)
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestAPIFacade;
import org.neo4j.rest.graphdb.RestGraphDatabase;
import org.neo4j.rest.graphdb.query.QueryEngine;
import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
import org.neo4j.rest.graphdb.util.QueryResult;
public class CypherQuery {
public static void main(String[] args) {
RestAPI graphDb = new RestAPIFacade("http://localhost:7474/db/data/");
QueryEngine engine=new RestCypherQueryEngine(graphDb);
QueryResult<Map<String,Object>> result = engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);
Iterator<Map<String, Object>> iterator=result.iterator();
if(iterator.hasNext()) {
Map<String,Object> row= iterator.next();
System.out.println("Total nodes: " + row.get("total"));
}
}
}
Error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/ws/rs/core/Response$StatusType
at org.neo4j.rest.graphdb.RestAPIFacade.<init>(RestAPIFacade.java:295)
at cz.mendelu.bp.CypherQuery.main(CypherQuery.java:19)
Caused by: java.lang.ClassNotFoundException: javax.ws.rs.core.Response$StatusType
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
Like #chrylis pointed out, your error seems to be the issue that you don’t have the required jars and hence the errors. Now from your comments, I see that you are having difficulties with understanding maven and dependencies. So here's a simple guide that I made for you.
[Understand that this is NOT a one stop guide and this program might
not run out of the box. Its running for me at the moment but it
depends on many things including what version of neo4j you are running
and several other configuration factors. Nonetheless, this should be sufficient to get
you started. ]
You need to have maven installed on your system. There are few cool tutorials on maven. One is here. https://www.youtube.com/watch?v=al7bRZzz4oU&list=PL92E89440B7BFD0F6
But like me if you want a faster way, the new Eclipse Luna comes with maven installed for it. So download the new eclipse luna if you wish. Even with older eclipse versions you can go to marketplace and install the maven for eclipse.
Once done, make a maven quickstart project and replace your pom.xml file with the one 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>rash.experiments</groupId>
<artifactId>neo4j</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>neo4j</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<repositories>
<repository>
<id>neo4j-release-repository</id>
<name>Neo4j Maven 2 release repository</name>
<url>http://m2.neo4j.org/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-rest-graphdb</artifactId>
<version>2.0.1</version>
</dependency>
</dependencies>
</project>
I assume that you have neo4j setup. I will not go into major details, but in the neo4j directory, under conf, in neo4j-server.properties file, you need to uncomment the line
org.neo4j.server.webserver.address = 0.0.0.0
This basically lets you access this server from your java code that you will run from another machine. After making this change, make sure that you restart your server and that its accessible to other machines. To test you can run http://ip.address.of.this.machine:7474 and the web portal that comes with neo4j should open up.
Note: I assume that you have some data in your database. This is
required otherwise the program will fail. If you need some sample
data, go to http://ip_address_of_your_neo4j_web_server:7474/ and load the
movie graph database that comes with the installation.
Now lets code. Make this class in the project that you created above.
package rash.experiments.neo4j;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import org.neo4j.rest.graphdb.RestAPI;
import org.neo4j.rest.graphdb.RestAPIFacade;
import org.neo4j.rest.graphdb.query.QueryEngine;
import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
import org.neo4j.rest.graphdb.util.QueryResult;
public class Neo4JRestTest
{
public static void main(String args[])
{
RestAPI graphDb = new RestAPIFacade("http://192.168.1.8:7474/db/data/");
QueryEngine engine = new RestCypherQueryEngine(graphDb);
QueryResult<Map<String, Object>> result = engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);
Iterator<Map<String, Object>> iterator = result.iterator();
if (iterator.hasNext())
{
Map<String, Object> row = iterator.next();
System.out.print("Total nodes: " + row.get("total"));
}
}
}
Now to run, you need to build your project first because maven will not download any of your jars that your specified in pom.xml until you have run it. So if you installed maven, go to the directory where you have your pom.xml and then write in the command line mvn clean package. This command will run and install all the dependencies and then will run your program. Since there are no test cases to run, it will succeed. But our goal was not to run any test cases. It was to download all the jars. Now that you have everything, you can run the java code and you will see your end results.
In case you are using maven from eclispe, then you right click your project and then do run as -> maven build. For the first time, a dialog will appear. Just write package in it and press enter. It will do the same things as above and bring all the jars. Then execute the program like you would above.
In case you get errors such as "No endpoint" or "error reading JSON", then understand that somehow the REST API is not able to read your graph.
check the property inside neo4j-server.properties. It should be whatever you are mentioning in your URL.
org.neo4j.server.webadmin.data.uri = /db/data/

Categories

Resources