this is a really simple java question. I am using Java 8 with eclipse kepler on a linux system. I've been trying to try out NIO.2. My code is:
package lucasTest;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
public class Lucas {
public static void main(String[] args) throws URISyntaxException{
URI u = new URI("./Lucas.java");
Path p = Paths.get(u);
}
}
I get the following error:
Exception in thread "main" java.lang.IllegalArgumentException: Missing scheme
at java.nio.file.Paths.get(Paths.java:134)
at lucasTest.Lucas.main(Lucas.java:10)
Please help!
Thanks,
Lucas
Your uri declaration is missing the scheme for files (file:///):
u = new URI("file:///./Lucas.java");
Path p = Paths.get(u);
should work. As an alternative you can try
Path p2 = Paths.get(".", "Lucas.java");
Related
When I am importing the HTML File according to the tutorialpoint link https://www.tutorialspoint.com/jsoup/jsoup_load_file.htm
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class jsoupTester {
public static void main(String[] args) throws IOException, URISyntaxException {
URL path = ClassLoader.getSystemResource("test.htm");
File input = new File(path.toURI());
Document document = Jsoup.parse(input, "UTF-8", "");
System.out.println(document.title());
}
}
I got this error when I run the program:
Exception in thread "main" java.lang.NullPointerException
at jsoupTester.main(jsoupTester.java:13)
Note: jsoupTester.java file and temp.htm are in the same location
May I know how to solve this issue? Your suggestions will be highly appreciated :)
Have you checked the website properly? the code documentation showed this
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class JsoupTester {
public static void main(String[] args) throws IOException, URISyntaxException {
URL path = ClassLoader.getSystemResource("test.htm");
File input = new File(path.toURI());
Document document = Jsoup.parse(input, "UTF-8"); // Only 2 parameters
System.out.println(document.title());
}
}
Error
Document document = Jsoup.parse(input, "UTF-8", ""); // 3rd parameter is not included in the documentation
As you can see the error is that you have another redundant parameter which I believe is causing the error. Remove that "" in your code and it will work fine. Hope that answers your question :)
Consider a simple Java file which creates a BufferedInputStream to copy a local file 1400-8.txt to Hadoop HDFS and print some dots as a progress status. The example is Example 3-3 from the Hadoop book here.
// cc FileCopyWithProgress Copies a local file to a Hadoop filesystem, and shows progress
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
// vv FileCopyWithProgress
public class FileCopyWithProgress {
public static void main(String[] args) throws Exception {
String localSrc = args[0];
String dst = args[1];
InputStream in = new BufferedInputStream(new FileInputStream(localSrc));
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
OutputStream out = fs.create(new Path(dst), new Progressable() {
public void progress() {
System.out.print(".");
}
});
IOUtils.copyBytes(in, out, 4096, true);
}
}
// ^^ FileCopyWithProgress
I compile the code and create the JAR file with
hadoop com.sun.tools.javac.Main FileCopyWithProgress.java
jar cf FileCopyWithProgress.jar FileCopyWithProgress.class
The above commands generate the files FileCopyWithProgress.class, FileCopyWithProgress$1.class and FileCopyWithProgress.jar. Then, I try to run it
hadoop jar FileCopyWithProgress.jar FileCopyWithProgress 1400-8.txt hdfs://localhost:9000/user/kostas/1400-8.txt
But, I receive the error
Exception in thread "main" java.lang.NoClassDefFoundError:
FileCopyWithProgress$1
To my understanding, the FileCopyWithProgress$1.class is due to the anonymous callback function the program declares. But since the file exists what is the issue here? Am I running the correct sequence of commands?
I found the issue so I am just posting in case it helps someone. I had to include the class FileCopyWithProgress$1.class in the JAR. The correct one should be
jar cf FileCopyWithProgress.jar FileCopyWithProgress*.class
Any idea why this works fine even on a Windows environment ?
Can you point me to some docs which explains why this works ?
I tried to look for it with no results.
package com.my.example;
import org.junit.Test;
import java.io.File;
public class TestPathCreation
{
#Test
public void testPathCreation() throws Exception {
final String path = "~/.my/dir";
System.out.println(path);
File f = new File(path);
System.out.println(f.mkdirs()); // true
}
}
I get the following error when I try to run
java -cp 'angus-sdk-java-0.0.2-jar-with-dependencies.jar:.' FaceDetect
I am following a tutorial for face detection in http://angus-doc.readthedocs.io/en/latest/getting-started/java.html . Below is my java code,
import java.io.IOException;
import org.json.simple.JSONObject;
import ai.angus.sdk.Configuration;
import ai.angus.sdk.Job;
import ai.angus.sdk.ProcessException;
import ai.angus.sdk.Root;
import ai.angus.sdk.Service;
import ai.angus.sdk.impl.ConfigurationImpl;
import ai.angus.sdk.impl.File;
public class FaceDetect {
public static void main(String[] args) throws IOException, ProcessException {
Configuration conf = new ConfigurationImpl();
Root root = conf.connect();
Service service = root.getServices().getService("age_and_gender_estimation", 1);
JSONObject params = new JSONObject();
params.put("image", new File("Downloads/IMG_1060.jpg"));
Job job = service.process(params);
System.out.println(job.getResult().toJSONString());
}
}
I don't understand the problem with it. I have tried all the answers in the stack overflow but nothing is working for me.
remove the single qoutes around the classpath:
java -cp angus-sdk-java-0.0.2-jar-with-dependencies.jar:. FaceDetect
Trying to run RecognizeUsingWebSocketsExample provided with IBM Watson SpeechToText Java SDK, but it's failing to create a valid RecognizeOptions object for the sample .wav file provided with the distribution:
Exception in thread "main" java.lang.IllegalArgumentException: When using PCM the audio rate should be specified.
at com.ibm.watson.developer_cloud.util.Validator.isTrue(Validator.java:38)
at com.ibm.watson.developer_cloud.speech_to_text.v1.RecognizeOptions$Builder.contentType(RecognizeOptions.java:95)
at com.ibm.watson.developer_cloud.speech_to_text.v1.RecognizeUsingWebSocketsExample.main(RecognizeUsingWebSocketsExample.java:30)
It appears that the contentType(HttpMediaType.AUDIO_WAV) is being misinterpreted as RAW. Here's the actual (unmodified from distro) code:
package com.ibm.watson.developer_cloud.speech_to_text.v1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import com.ibm.watson.developer_cloud.http.HttpMediaType;
import com.ibm.watson.developer_cloud.speech_to_text.v1.model.SpeechResults;
import com.ibm.watson.developer_cloud.speech_to_text.v1.websocket.BaseRecognizeCallback;
/**
* Recognize using WebSockets a sample wav file and print the transcript into the console output.
*/
public class RecognizeUsingWebSocketsExample {
private static CountDownLatch lock = new CountDownLatch(1);
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
SpeechToText service = new SpeechToText();
service.setUsernameAndPassword("<username>", "<password>");
FileInputStream audio = new FileInputStream("src/test/resources/speech_to_text/sample1.wav");
RecognizeOptions options = new RecognizeOptions.Builder()
.continuous(true)
.interimResults(true)
.contentType(HttpMediaType.AUDIO_WAV)
.build();
service.recognizeUsingWebSocket(audio, options, new BaseRecognizeCallback() {
#Override
public void onTranscription(SpeechResults speechResults) {
System.out.println(speechResults);
if (speechResults.isFinal())
lock.countDown();
}
});
lock.await(1, TimeUnit.MINUTES);
}
}
I'm using 3.0.0-RC2 snapshot. No problems running examples which do not use RecognizeOptions, like SpeechToTextExample. Thx.
-rg
Sorry, false alarm. I recreated the example project from scratch and it compiled and ran without a hitch. Must have been some weirdness with my Eclipse setup.