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?
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 recently used decode_qr from this FEX submission to decode my QR code. It ran quite well one or two weeks ago, but today it generate an error for me:
Undefined function or variable 'BufferedImageLuminanceSource'
Error in decode_qr (line 34);
source = BufferedImageLuminanceSource(jig);
I just checked the zxing repository and found that some files were updated several days ago. So I guess the path of some imported file from the package has been changed.
Here is the importing code from the decode_qr function:
import com.google.zxing.qrcode.*;
import com.google.zxing.client.j2se.*;
import com.google.zxing.*;
import com.google.zxing.common.*;
import com.google.zxing.Result.*;
How can I get it to work again? Do I need to change the import paths?
Here's what I did to get it to work (Win 10 x64, R2017b, ZXing 3.3.1):
Downloaded the latest prebuilt .jar artifacts from Sonatype:
core.
javase.
Added the files to my dynamic java classpath using javaaddpath:
javaaddpath('G:\core-3.3.1.jar');
javaaddpath('G:\javase-3.3.1.jar');
% Verify using: javaclasspath('-dynamic');
Note:
To add folders to the static path, which MATLAB loads at startup, create a javaclasspath.txt file, as described in Static Path.
Generated some example QR code using unitag.io:
Tried to decode it using Lior Shapira's decode_qr:
>> out = decode_qr(qr)
out =
'https://stackoverflow.com/users/3372061/dev-il'
Full code:
function out = q47223578()
javaaddpath('G:\core-3.3.1.jar');
javaaddpath('G:\javase-3.3.1.jar');
% Verify using: javaclasspath('-dynamic');
qr = imread('https://i.stack.imgur.com/mA4eP.png');
out = decode_qr(qr);
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
Hello i read that I have to use the java client library in java to get the revisions list in google drive using google api. I use netbeans. I search for this question and I try to add the bib but i haven't get success. To download the library i visited this link and downloaded the "latest". but after add the jar files I get the message error that the package doesnt exist. Please someone help me!
Yes! I try this but it show the same erros. I add the lib but netbeans (8) show the error in the three lines code: "package [name] doesn't exist".
The three lines are these:
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Revision;
import com.google.api.services.drive.model.RevisionList;
obs: the full code, taken in the google developers site is:
package javaapplication8;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.model.Revision;
import com.google.api.services.drive.model.RevisionList;
import java.io.IOException;
import java.util.List;
public class MyClass {
private static List<Revision> retrieveRevisions(Drive service,
String fileId) {
fileId = "1XpNdeTFBr2KygyfPtlowBvkpcaJJzjgLckrGjp5oOhg0";
try {
RevisionList revisions = service.revisions().list(fileId).execute();
return revisions.getItems();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
return null;
}
}
If it is the error like package is missing, please check whether you have created a folder named com in your project or not. Also, every dot (.) indicates the levels of packages that you use in your project. Please check that com folder followed by the other folder names after every dot are present or not. I think this might help you. I answered your question based on my understanding (i.e, package issue which will be resolved by creating folders in your project. folders means packages). I am sorry if I gave you the wrong answer.
I'm discovering the jni4net. This is the technology used to provide the bridge between Java and .NET. So, I created new Eclipse Java project and copied the sample code from jni4net-0.8.6.0-bin/samples/myCSharpDemoCalc->MyCalcUsageInJava.java into this project. However the code cannot be compiled because two imports "mycsharpdemocalc.DemoCalc" and "mycsharpdemocalc.ICalc" cannot be found. I don't understand how to integrate/import mycsharpdemocalc.c into the Java project so that the code could be compiled.
import net.sf.jni4net.Bridge;
import java.io.IOException;
import mycsharpdemocalc.DemoCalc;
import mycsharpdemocalc.ICalc;
public class MyCalcUsageInJava {
public static void main(String arsg[]) throws IOException {
Bridge.init();
Bridge.LoadAndRegisterAssemblyFrom(new java.io.File("MyCSharpDemoCalc.j4n.dll"));
ICalc calc = new DemoCalc();
final int result = calc.MySuperSmartFunctionIDontHaveInJava("Answer to the Ultimate Question of Life, the Universe, and Everything");
System.out.printf("Answer to the Ultimate Question is : " + result);
}
}
There is ReadMe in each sample directory.
You have to use proxygen tool to generate the proxies (which are used in the java code).
There is generateProxies.cmd batch to do that.
More complex things may need config file for proxygen.
Also there is community Wiki