(I am new to Java, I don't know what 'classes' or 'api's' are.)
I was trying to compile (javac -g Sphinx.java) this code:
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.SpeechResult;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
public class Sphinx {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
configuration.setAcousticModelPath("models/en-us/en-us");
configuration.setDictionaryPath("models/en-us/cmudict-en-us.dict");
configuration.setLanguageModelPath("models/en-us/en-us.lm.bin");
PrintWriter pw = new PrintWriter(new PrintWriter("status.txt"));
LiveSpeechRecognizer recognizer = new LiveSpeechRecognizer(configuration);
recognizer.startRecognition(true);
pw.print("running");
SpeechResult result = recognizer.getResult();
recognizer.stopRecognition();
pw.print("stopped");
pw.close();
PrintWriter pw2 = new PrintWriter(new PrintWriter("result.txt"));
pw2.println(result);
pw2.close();
}
}
And I got this message:
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
So, I re-compiled with -Xlint:deprecation, like it told me to, and it didn't give me any errors this time, so I'm assuming the compiler was finished, and that it compiled successfully.
And then I look, and there's no .jar file, just a new .class file.
Now, I don't really know much about the java compiler, I was just told online that it would give me an executable for the code I had written, which in this case is a .jar file.
I don't know if the compiler sends newly created executables to a special system directory or what, but it's not here, and I don't know why.
Would someone more knowledgeable with Java please give me some context here.
You need just add the next step - look at: docs.oracle.com/javase/tutorial/deployment/jar/build.html
jar cf jar-file input-file(s)
Related
i'm trying to use itext (5.5.13) in IBM i (AKA iseries, Power, long ago AS/400). It could be done embedding java code into RPG ILE procedures, or executing plain java. We use Apache POI for Excel for a while, and it works well. We are testing itext now, but some issue persist yet.
Given that, I'm trying to test itext in plain java into IBM i. I prepared a very simple example, taken from listing 1.1 of "Itext in action", and run it. It seems to work well, but nothing is generated. No pdf file results. And no error appears while running.
am i forgetting something? are there some other aspects to take in account?
here is the code:
package QOpenSys.CONSUM.Testjeu;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class test1{
public static final String filePdf = "/QOpenSys/MyFolder/Testjeu/PdfRead1.pdf";
public static void main(String[] args)
throws DocumentException, IOException
{
///QOpenSys/MyFolder/Test/WrkBookRead1.pdf
//pdfDocument = new_DocumentVoid()
Document pdfDocument = new Document();
//pdfWriter = get_PdfWriter( pdfDocument: pdfFilePath);
PdfWriter.getInstance(pdfDocument, new FileOutputStream( filePdf ));
// jItxDocumentOpen( pdfDocument );
pdfDocument.open();
//pdfParagraph = new_PdfParagraphStr( PhraseString );
Paragraph jItxParagraph = new Paragraph("Hola, pdf");
//addToDocPg = jItxDocumentAddParagraph( pdfDocument: pdfParagraph );
pdfDocument.add(jItxParagraph);
//jItxDocumentClose( pdfDocument );
pdfDocument.close();
}
}
Solved. As said before, there was a first issue: it seems java function ran well because not errors/warnings were visible at qshell. It was false: errors were sent to outq, and were available at spool file. Being reviewed, it was a simple classpath issue. It required a full day to figure out what failed locating classpath.
Now it works, and pdf is created. I ran it on qshell, declaring environment variables for java_home (three jvm are executed concurrently by several applications), for classpath, and a couple required for tracing. Classpath declares first my class and secondly itext classes. Remaining classes comes from JRE. I have a full list of classes loaded by class loader. I hope it will help to find what fails in our embedded RPG ILE call to itext.
I am learning to program in java. I have programmed in other languages. I have tried import java.net.*; at the start of my code file. But it ghosts out. I have looked in my External Libraries directory and it is not there. I found that java.net was deprecated in 201x <-- some recent year. I am using jdk 10. I am using IntelliJ IDE. I have gotten some import statements to work.
I saw on github that they took over hosting oracle classes that were deprecated at Oracle.
I know I have to use the classpath command if I put the .jar or the .zip file containing the class in another directory. I have searched my laptop and i don't have any other .jar or .zip files other than specific other programs I have installed which also don't contain the java.net class (e.g. Aptana Studio 3)
I am using Mac with OS Sierra.
package com.robinhoodcomputer.myfirstprojects;
import java.io.FileReader; <<-- these import ok
import java.io.*;
import java.net.*; <<--- these don't import
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class Myreader {
public static void main(String[] args) throws Exception {
int intVal;
char myChar;
String st;
File file = new File("/java/file");
BufferedReader br = new BufferedReader(new FileReader(file));
// FileReader fr = new
FileReader("/java/HelloWorld/resources/file.txt");
while ((intVal = br.read()) != -1) {
myChar = (char) intVal;
System.out.print(myChar);
}
myHostName = getLocalHost(); <<-- this doesn't show being available
}
}
I have searched and cannot find any articles that actually do anything but explain how to connect to the class file and that you have to import it in your code. No one talks about getting the java.net class itself.
I found one reference to jdk.net in an oracle jdk 10 API specification page.
What am I suppose to use to get the IP address using a hostname in java these days??
thx
P.S. I know this code really doesn't have anything to do with networking, most of it is just reading a file and displaying what's in the file. I wanted to use this code file just to try getting an IP address also. My question is mainly just about making the import statement to work. thx
Your imports are grayed out, since you do not call any method of the imported libraries. As soon as you start using the getLocalHost() method properly, the import will not be grayed out any more. This is a convenience functionality of your IDE it seems.
getLocalHost() is a method of InetAddress and can't be just called without such an instance.
Look at this question for how to use this:
java InetAddress.getLocalHost(); returns 127.0.0.1 ... how to get REAL IP?
If anyone has any experience working with Yaml (.yml extension files), I could use some assistance. I am trying to write a little program that reads .yml files from a folder I made on my desktop.
import java.io.IOException;
import java.io.InputStream;
import org.yaml.snakeyaml.Yaml
public class readYaml {
public static void main(String[] args) throws IOException {
Yaml yaml = new Yaml();
try (InputStream in = readYaml.class.getResourceAsStream("/Collections.yml")) {
Question question = yaml.loadAs(in, Question.class);
System.out.println(question);
}
}
}
EDIT
I created a resources folder in my project file in response to an error on line 8 stating "This is coming from an error I am getting from an error I am getting on line 8, stating "The import org.yaml.snakeyaml.Yaml cannot be resolved"", but now I am getting this error. I think it will be best explained in screen shots. enter image description here
and this happens on my project folder
enter image description here
I hope this makes things clearer to understand.
Before I start, I'd like to say that I've spent 4 hours today, 6 hours yesterday and 3 hours before that researching this issue. I've read every post I can find, followed every instruction to the letter, restarted my project, reinstalled my IDE (Netbeans) and even fresh installed my OS, and I haven't found a single piece of helpful advice, so I figured I needed to ask for help.
AND YES, I HAVE PUT THE FILE IN THE RIGHT LOCATION
... As a matter of fact, I've put the file in EVERY location. There's a copy in every folder inside my project and also a copy in the overall Projects folder, and also in My Documents. I've checked and changed and defaulted the root directory many times. PLEASE don't tell me to just use an exception handler. The file the program reads in is guaranteed to exist and contain something.
So here's my question:
I'm trying to input and read from a file, however, the result is always that the file can't be found. Here's an example of my code (and it really is down to this atm):
package project2;
import java.io.FileReader;
import java.io.*;
import java.util.Scanner;
public class Project2 {
public static void main(String[] args) {
FileReader inputFile = new FileReader(args[0]);
}
}
Here are two of the errors I get (I also get Filenotfound errors, but I don't think I need to add that):
Exception in thread "main" java.lang.RuntimeException: Uncompilable source
code - unreported exception java.io.FileNotFoundException; must be caught or
declared to be thrown
at project2.Project2.main(Project2.java:14)
C:\Users\jarre\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:
Java returned: 1
BUILD FAILED (total time: 1 second)
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at project2.Project2.main(Project2.java:24)
C:\Users\jarre\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:
Java returned: 1
BUILD FAILED (total time: 0 seconds)
That's it. The file name comes from the arguments, and I have tried every possible variation of the name. I have tried naming the file outside of the arguments, as just the file name itself and also with an explicit file path.
Using a scanner won't let me read anything in. FileReader won't even run.
The text file has no special formatting or characters, and I've used the one I was supplied with and multiple that I hand typed just in case there was an issue with the one I was given. I have also made sure that ".txt" is never read or used twice (I keep my extensions on, anyway).
I have checked attributes and permissions of all files and the Netbeans program itself. I've also made sure that the text files were included in the project build.
I am not using any additional code right now, as I can't do anything until I'm sure that I can read in a file, and then output one as well. I also know that the text files aren't corrupt because I can read them in Python just fine, however, I have to use Java and I have to use Netbeans.
This is a new problem for me, I've always been able to read in files fine, and I've exhausted my options. I really need some help if anyone has any ideas.
The first exception (java.lang.RuntimeException: Uncompilable source
code) is thrown because the code that you have shown us is not valid java source code.
new FileReader(args[0]) is declared as throwing FileNotFoundException and according to the rules of the java language you either have to catch this exception or declare your main method as throwing this exception.
One way to fix this problem is to write your main method like this:
public static void main(String[] args) throws FileNotFoundException {
FileReader inputFile = new FileReader(args[0]);
}
It seems that you have solved this issue because the second exception (java.util.NoSuchElementException: No line found) is thrown by the Scanner.nextLine() method if you try to read past the end of the file.
Since you have not shown any code using the Scanner class it's hard to tell where to problem is in this case.
As a matter of fact, I've put the file in EVERY location. There's a copy in every folder inside my project and also a copy in the overall Projects folder, and also in My Documents.
Don't do that. You are creating a mess with files that will be hard to cleanup. If you want to know which file your program is reading then adding the following simple line tells you the exact path and filename:
System.out.println(new File(args[0]).getAbsolutePath());
Have you ever tried with a simple, minimal example like this:
package project2;
import java.io.FileReader;
import java.io.*;
import java.util.Scanner;
public class Project2 {
public static void main(String[] args) {
System.out.println(new File(args[0]).getAbsolutePath());
FileReader inputFile = new FileReader(args[0]);
try (Scanner s = new Scanner(inputFile)) {
while (s.hasNextLine()) {
System.out.println(s.nextLine());
}
}
}
}
It should print out the name of your file with the complete path and then the contents of the file line by line.
I don't think Java is messing around with you a not found file is a not found file, please elaborate more in this issue by screens of files and directories you are working on.
I would like you to consider take a look at the following:
FileReader
Path of projects on Netbeans
I hope this helps may the code be with you.
This reads a file with no problem. I'll assume you're running JDK 8.
/**
* Read a file example
* User: mduffy
* Date: 4/21/2017
* Time: 7:48 AM
* #link http://stackoverflow.com/questions/43529600/java-nothing-will-read-in-this-file
*/
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Project2 {
public static void main(String[] args) {
if (args.length > 0) {
BufferedReader reader = null;
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(args[0]))) {
bufferedReader.lines().forEach(System.out::println);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Usage: Project2 <file>");
}
}
}
Here's the input file I used:
line1
line2
hello, michael
line 4
Here's the output I got:
java Project2 .\src\main\resources\test.txt
line1
line2
hello, michael
line 4
Process finished with exit code 0
This question already has answers here:
How to use 3rd party packages in Java
(3 answers)
Closed 6 years ago.
I am new in Java programming language and i want to use a library by importing their packages . Can anyone tell me how can i import packages in Java using text editor?
I found this library in github and i wanted to use their packages for my java code i am developing by using import. I tried just to call these packages on my code by using import but in compiler there was an error which states: packages not found.
import com.tiemens.secretshare.main.cli.*;
import com.tiemens.secretshare.main.cli.*;
import java.io.*;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static java.lang.Integer.min;
import static java.util.Arrays.copyOfRange;
public class Shamir {
//The encoding that will be used when splitting and combining files.
static String encoding = "ISO-8859-1";
//The number of bytes per piece (except maybe the last one)!
static int pieceSize = 128;
//Mode 0 for strings, 1 for ints.
public static ArrayList<String> shamirSplit(String inputString, int numPieces, int minPieces, int mode) {
String type = "-sS";
if (mode == 1) {
type = "-sN";
}
ArrayList<String> parts = new ArrayList<>();
String[] splitArgs = {"-n", Integer.toString(numPieces), "-k", Integer.toString(minPieces), type, inputString, "-primeNone"};
MainSplit.SplitInput splitInput = MainSplit.SplitInput.parse(splitArgs);
MainSplit.SplitOutput splitOutput = splitInput.output();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
splitOutput.print(ps);
String content = baos.toString(); // e.g. ISO-8859-1
BufferedReader reader = new BufferedReader(new StringReader(content));
String line;
int i = 0;
try {
while ((line = reader.readLine()) != null && i < numPieces) {
if (line.startsWith("Share (x")) {
i++;
parts.add(line.trim());
}
}
} catch (Exception e)
So my class i want to implement is Shamir class but i need to import com.tiemens.secretshare.main.cli.*;
Can anyone tell me how to make this package work for my Shamir class?
I am guessing you aren't using maven. Download the jar files for packages you want to import and put then in your build path
If I am not mistaken, when I do what you did:
import com.tiemens.secretshare.main.cli.*;
public class Foo {
}
and then try to compile using javac Foo.java, I get:
Error:(2, 1) java: package com.tiemens.secretshare.main.cli does not exist
This means that when the compiler javac is trying to compile your class (Shamir.java) it needs either the source files or the bytecode (class files) for the classes in the package com.tiemens.secretshare.main.cli. Since you seem to have neither, the compilation fails.
Thus, you need the jar file that contains the classes they you want to compile against. There are two ways to achieve this:
Use Maven. But that means you need to learn Maven. That's life. Use mvn repo to compile against.
If you think it is too much of work to learn Maven, you will need to build secretshare code on GitHub yourself. This means you will need to learn gradle. Again, that's life.
Too bad you couldn't download the JAR file as a "release download" for this project.
Download the jar for your library and include it in your project classpath. Then you can import it in your class.
For setting the classpath use this link