I am trying to send data via usb with Java.And I decided to use the jSerialComm library. I downloaded the required jar file and imported it correctly.
The whole code :
import com.fazecast.jSerialComm.SerialPort;
public class Try{
public static void main(String[] args) throws IOException {
SerialComm ports[] = SerialComm.getCommPorts();
}
}
There is no problem with this row :
import com.fazecast.jSerialComm.SerialPort;
But there is a problem here :
SerialComm ports[] = SerialComm.getCommPorts();
Error message : SerialComm cannot be resolved to a type.
And this is advice : Create class 'SerialComm'
You
import com.fazecast.jSerialComm.SerialPort
But not
import com.fazecast.jSerialComm.SerialComm
SearialPort class is different than SerialComm, you need to import SerialComm class as well.
If you couldn't find it, it means your jar file is not compatible with your snippet code.
you should import this way :
import com.fazecast.jSerialComm.SerialComm
you could check out this if you want to more information :instalation
look at this is a example of send data via usb with Java
check out this :jSerialComm/package-summary
Related
Follow up of create `KafkaServer` from Java
I am creating a KafkaServer from Java (well Clojure really but given a working Java example it is straightforward to translate).
I am not able to pass anything but an empty sequence. How can I write the equivalent of this line in Java?
https://github.com/apache/kafka/blob/cb674e5487f3f56647546b323dfe4fd45ccf0186/core/src/main/scala/kafka/server/KafkaServerStartable.scala#L27
val reporters = KafkaMetricsReporter.startReporters(new VerifiableProperties(serverProps))
Or, better yet, is there a Java/Clojure API for creating reporters?
The code I gave you in create KafkaServer from Java should work. I just tried it in 0.11.0.1 (the version you mentioned in the other question) and it works fine.
Full snippet:
package main;
import java.util.Properties;
import kafka.metrics.KafkaMetricsReporter;
import kafka.metrics.KafkaMetricsReporter$;
import kafka.utils.VerifiableProperties;
import scala.collection.*;
public class Reporters {
public static void main(String[] args) {
Properties props = new Properties();
Seq<KafkaMetricsReporter> reporters = KafkaMetricsReporter$.MODULE$.startReporters(new VerifiableProperties(props));
}
}
I want to use the Twilio API to allow users from my web application to make calls or send messages. So far I only wrote this basic code:
import com.twilio.sdk.TwilioRestClient;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
public class Main {
public static final String ACCOUNT_SID = "ACXX";
public static final String AUTH_TOKEN = "XX";
public static void main(String[] args) throws URISyntaxException {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.creator(new PhoneNumber("+40742000000"), new PhoneNumber("+40742000000),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
System.out.println(call.getSid());
}
This is the place where my JAR is stored now(the end of Referenced Libraries)
It just says that the imports Twilio cannot be resolved. I have Java version 8, so it should be working like this. I also download the JARs and followed the instalation from this page. Still not working. Does any of you have an idea how to make it work?
Twilio developer evangelist here.
If you are using the version 7 Twilio Java library then you no longer need to import com.twilio.sdk.TwilioRestClient;. In fact, that is no longer there, so this might be causing your import issues.
Also, make sure you have only one version of the JAR in your project. And make sure to keep up to date, the current version, as of writing, is 7.14.4.
Check out the docs on making a call with Twilio in Java. You'll find the example looks like this:
import java.net.URI;
import java.net.URISyntaxException;
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Call;
import com.twilio.type.PhoneNumber;
public class Example {
// Find your Account Sid and Token at twilio.com/user/account
public static final String ACCOUNT_SID = "your_account_sid";
public static final String AUTH_TOKEN = "your_auth_token";
public static void main(String[] args) throws URISyntaxException {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Call call = Call.creator(new PhoneNumber("+14155551212"), new PhoneNumber("+15017250604"),
new URI("http://demo.twilio.com/docs/voice.xml")).create();
System.out.println(call.getSid());
}
}
Give that a go and let me know if it helps.
If anyone trying to use twilio [ I am using with spring boot ], please always make sure you pick up the latest version <version>7.50.1</version> as of now.
I used earlier version and it didn't work. As soon as I upgraded that to 7.50.1
it worked.
I hope it helps you.
I am trying to read a file (which contains my data) in my JUnit code.
I have a source folder named "test" and under it are the two packages below
com.junit.codes (JUnit codes)
com.junit.codes.data (csv Files i.e. myData.csv)
My problem is I cant access the files under com.junit.codes.data.
I have tried using classLoader but it does not work.
Can anyone help me with this problem?
Presuming you are using a Maven based setup, do the following:
As non-Java files by default is not copied to the "target" folder in the "compile" phase you should add your csv-file to src/test/resources/com/junit/codes/data
From your test class you should now be able to do getClass().getResourceAsStream("./data/myData.csv") to open an input stream to read the data from.
Example:
package com.junit.codes;
import org.junit.Test;
import java.io.InputStream;
import static org.junit.Assert.assertNotNull;
public class ReadTest {
#Test
public void name() throws Exception {
try(final InputStream inputStream = getClass().getResourceAsStream("./data/myData.csv")) {
assertNotNull(inputStream);
}
}
}
I am testing a few Java API, I've created my project called 'MyLearning' where all my src files are located, in src I created another Package callede 'myfiles', now when I import the java.nio.file.Files API, IntelliJ doesn't show me suggestions for this class. But in the main package i.e src folder, the suggestion works totally fine.
Example:
The above picture shows my main src folder, where the Files API works totally fine.
But then in the new Package that I've created i.e myfiles, it is showing error on retrieving the methods of Files API. Error is
Cannot resolve symbol 'exists'
Can anyone tell me what could be the poblem here?
You have to put method calls inside a method.
public void foo()
{
Files.exists(path);
}
I also noticed that one of the tags you put is intellij-14. The latest version of IntelliJ is 2016.2.
You have to call it in a method, not in the class
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
Path path = Paths.get("C:\\log.txt");
System.out.println(Files.exists(path));
}
}
I'm using json4s in a play project, and I'm also using a library called sbt-buildinfo which generates Scala source from your build definitions.
Now, in the sbt-buildinfo library the say you need to add some line of code: buildInfoOptions += BuildInfoOption.ToJson so you can use .toJson, but from some reason I can use .toJson.
this is how I do it:
import _root_.util.{AuthenticatedAction}
import buildinfo.BuildInfo
import com.google.inject.Inject
import org.json4s.BuildInfo
import play.api._
import play.api.mvc._
class AppInfo #Inject()(implicit configuration: Configuration) extends Controller {
def appVerion = AuthenticatedAction {
Ok(BuildInfo.toJson)
}
but the import buildinfo.BuildInfo stays gray....so it looks like I'm not using it. I refreshed the build.sbt and all, what could it be?
You have multiple imports to a BuildInfo object. org.json4s.BuildInfo will probably shadow your buildInfo.BuildInfo import and therefore, it does not have the required member. Try writing out the entire package name that you need:
Ok(buildinfo.BuildInfo.toJson)