How to compile Java class with external library in Windows command line? - java

I have an assignment: Write a client program Permutation.java that takes an integer k as a command-line argument; reads in a sequence of strings from standard input using StdIn.readString()...
Here is my code:
public class Permutation {
public static void main(String[] args) {
int k = Integer.parseInt(args[0]);
String[] inputStrings = StdIn.readStrings();
}
}
How to compile it in Windows command line?
I tried
javac Permutation.java
But got an error
error: cannot find symbol
String[] inputStrings = StdIn.readStrings();`
I use IntelliJ IDEA, external library StdIn is added to the project.

To compile it with the library you have to add it to the classpath. This is accomplished with the -cp argument followed by the library which contains your referenced class.
javac -cp StdIn.jar Permutation.java
To run it with command line, similarly use
java -cp StdIn.jar Permutation
See also https://docs.oracle.com/javase/8/docs/technotes/tools/windows/javac.html

Related

Compiling and running a simple Java program using java-algs4

I'm learning Java through the this Princeton course https://algs4.cs.princeton.edu/home/. I've create a simple Java program called Ortho.java using the Java provided by the course (that I installed using this tutorial: https://algs4.cs.princeton.edu/linux/). The program receives two .txt input files, it reads those as a list of strings, and print their first value.
Here's the program:
package edu.princeton.cs.algs4;
import java.util.Arrays;
class Ortho{
public static void main(String args[]){
// Read files from input and print statements
In in = new In(args[0]);
String[] list = in.readAllStrings(); //parse the first text input
while (!StdIn.isEmpty()) {
String key = StdIn.readString();
System.out.print(key);
}
}
}
I run this command on the shell to compile my program, this creates a file called Ortho.class:
javac-algs4 Ortho.java
This is the command I use to run the program:
java-algs4 Ortho.class text_1.txt < text_2.txt
But I always get this error:
Error: Could not find or load main class Ortho.class
Caused by: java.lang.ClassNotFoundException: Ortho.class
What do I need to do to properly compile and run this program with the algs4 Java lib provided by the course?
This is my project structure:
dir/
Ortho.java
Ortho.class
text_1.txt
text_2.txt
Run the program like this :
java algs4.Ortho text_1.txt < text_2.txt
Let me know if this worked for you or no.

Unable to run java with command line arguments

I have been trying to run Java with command line arguments, but for some reason the class can not be found. I am very certain the directory is correct. Is there any way to fix this?
CLDemo.java file
public class Demo {
public static void main(String[] args) {
System.out.print("It works!!!");
}
}
You need to do cd out\production before java CLDemo.
The default compile output of IntelliJ IDEA is under out\production folder, and Java needs to run at the corresponding package (folder) of your compile output.

Can't use the Scanner class

I just started studying JAVA. I'm trying something very simple: Request a argument from the user on the prompt. I made this code:
import java.util.Scanner;
public class Request
{
public static void main (String[] args)
{
Scanner s = new Scanner(System.in);
System.out.println("What's your name?");
String name = s.nextLine();
System.out.println("Welcome " + name);
}
}
then I compile it, and when I execute it with
Java -cp Request
Instead of a prompt expecting a parameter
It prints all the options of the JAVA command as if I had coded this:
java -help
Why is that happening?
Use java Request, or maybe java -cp . Request ; the -cp option provides the JVM with a classpath, which should be a list of jars or directories containing .class files.
As Klitos mentions, you can set the CLASSPATH environment variable to provide a default classpath. Setting it to include the current directory . is common practice and spare you from having to use -cp . when invoking Java.
In Windows, the variable is %CLASSPATH% and is a ;-separated list of windows pathes.
In Linux (and MacOS AFAIK), the variable is $CLASSPATH and is a :-separated list of unix pathes.

Split rule of args in main function in Java?

Here is a strange problem I ran into:
I create a single program to print all the received args, Here is the code:
public class Test {
public static void main(String[] args) {
for (int i = 0; i < args.length; i ++) {
System.out.println(args[i]);
}
}
}
Then I built a jar file of it and ran the following command:
java -jar test.jar test&1
However, it didn't print "test&1" as expected. The result of it is:
test
'1'is not recognized as an internal or external command,operable program or batch file.
So my question is: what is the seperation of args? If I really need to receive "test&1", what should I do?
Thanks!
It's nothing to do with Java. The & character is special to the Windows shell (I can tell it's Windows from the error message): It separates two commands on one line, so what you're doing is telling the shell to run java -jar test.jar test and then run 1. If you want to pass test&1 to Java, you'll have to put it in quotes:
java -jar test.jar "test&1"
The & is also special on *nix shells (but in a different way, it runs the command in a sub-shell). There, you could use quotes as above, or put an \ before the & instead. But not on Windows.

Passing on command line arguments to runnable JAR [duplicate]

This question already has answers here:
How do I pass parameters to a jar file at the time of execution?
(5 answers)
Closed 5 years ago.
I built a runnable JAR from an Eclipse project that processes a given XML file and extracts the plain text. However, this version requires that the file be hard-coded in the code.
Is there a way to do something like this
java -jar wiki2txt enwiki-20111007-pages-articles.xml
and have the jar execute on the xml file?
I've done some looking around, and all the examples given have to do with compiling the JAR on the command line, and none deal with passing in arguments.
Why not ?
Just modify your Main-Class to receive arguments and act upon the argument.
public class wiki2txt {
public static void main(String[] args) {
String fileName = args[0];
// Use FileInputStream, BufferedReader etc here.
}
}
Specify the full path in the commandline.
java -jar wiki2txt /home/bla/enwiki-....xml
You can also set a Java property, i.e. environment variable, on the command line and easily use it anywhere in your code.
The command line would be done this way:
c:/> java -jar -Dmyvar=enwiki-20111007-pages-articles.xml wiki2txt
and the java code accesses the value like this:
String context = System.getProperty("myvar");
See this question about argument passing in Java.
You can pass program arguments on the command line and get them in your Java app like this:
public static void main(String[] args) {
String pathToXml = args[0];
....
}
Alternatively you pass a system property by changing the command line to:
java -Dpath-to-xml=enwiki-20111007-pages-articles.xml -jar wiki2txt
and your main class to:
public static void main(String[] args) {
String pathToXml = System.getProperty("path-to-xml");
....
}
When you run your application this way, the java excecutable read the MANIFEST inside your jar and find the main class you defined. In this class you have a static method called main. In this method you may use the command line arguments.

Categories

Resources