Using command lines with Eclipse - java

I am trying to compile and run this program by using eclipse. How do I do this?
I cant include the command line in the program. how do i use eclipse to run the program by using this command line??
I know theres another way of making the program by using Scanner but how do i run this one properly???
Cant post an image. Just joined.
INPUTTING FROM A FILE
Display a text file.
/*To use this program, specify the name
of the file that you want to see.
For example, to see a file called TEST.TXT,
use the following command line.
java ShowFile TEST.TXT */
import java.io.*;
class ShowFile {
public static void main(String args[])
throws IOException
{
int i;
FileInputStream fin;
try {
fin = new FileInputStream(args[0]);
} catch(FileNotFoundException exc) {
System.out.println("File Not Found");
return;
} catch(ArrayIndexOutOfBoundsException exc) {
System.out.println("Usage: ShowFile File");
return;
}
// read bytes until EOF is encountered
do {
i = fin.read();
if(i != -1) System.out.print((char) i);
} while(i != -1);
fin.close();
}
}

Right click on your program, Hover over to Run As and Click Run Configurations
You will see a dialog box, now click on Arguments tab and specify the command line arguments.

I am not sure what you are asking, but to view the output of your program in the console:
You can do:
Window > Show View > Console
If you want to you want to compile from the command line you can use javac.

Right click on your program, Hover over to Run As and Click Run Configurations You will see a dialog box, now click on Arguments tab and specify the command line arguments.
On Program Arguments simply write file name OR file Address No need to write java ShowFile in your case it will be TEST.TXT .
Note : Make sure that TEST.TXT file is in your project folder

Related

Getting text from .bat file in CMD

I have created a OpenNotepad.bat file with text stating "C:\windows\system32" notepad.exe and saved it to my desktop. I have created a Java class. What do I have to type in the cmd to get that text from the .bat file?
Is there a specific command that I should use to get the command prompt to display that text?
Here is my Java code:
public class OpenBatchFile {
public OpenBatchFile() {
super();
}
public static void main(String[] args) {
//Get Runtime object
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("cmd /c start Desktop:\\OpenNotePad.bat");
}
catch (IOException e) {
System.out.println(e);
}
}
}
I am totally new to this. I referred to this tutorial:
Execute batch file from Java Code using Runtime class
When in cmd.exe console, type the command help which will display all possible commands you can run in a cmd prompt or batch... If you read through each, you will notice two that stand out:
MORE Displays output one screen at a time.
TYPE Displays the contents of a text file.
This will display the content of any file if you use them like:
type filename.txt
more filename.txt
or
type "c:\program files\some dir\filename.txt"
more "c:\program files\some dir\filename.txt"

About citing external files in java application

I am writing a java application, in which I am automatically importing external csv files in background to do the computation. But the problem is that I am using "absolute" file path in my java program, the generated jar file will not work in another computer. Is there anyway in java to use a kind of "working directory path" so that I can still run the jar file in another computer as long as I put the csv files I'd like to import in the same folder with the jar file?
Thanks!
You can read a file using its name like
try (BufferedReader br = new BufferedReader(new FileReader("text.txt"))) {
String line;
while ((line=br.readLine())!=null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
}
Here text.txt should be in the same working directory where the jar was executed.
You can also read the directory name from the command line, using the command line arguments like
public static void main(String[] args) {
//check if there were any command line arguments
if (args.length > 0) {
// args[0] is the first command line argument unlike C where args[0] would give u the executable's name
} else {
System.err.println("Usage: java -jar <jar_name> [directory_names..]");
}
}
You can also have a configuration file such as a properties file to read the directory names.
new File(".") give you the relative path
you can write relative path like that :
File file = new File(".\\CSVs\\myfile.csv");
System.getProperty("user.dir") will return you the working directory.
System.getProperty("user.dir")+"\\myfile.txt"
More informations here :system properties, oracle docs

execute jar file in java program

I want to create an java program to compress an css file using YUI
I am new learner in java.
My Code is:
import java.io.BufferedInputStream;
import java.io.IOException;
public class Run extends Object
{
public static void main(String args[]) throws IOException, InterruptedException
{
System.out.println("Calling jar");
Process p = Runtime.getRuntime().exec("java -Xmx32m -jar yui.jar in.css");
BufferedInputStream bis = new BufferedInputStream(p.getInputStream());
synchronized(p)
{
p.waitFor();
}
System.out.println(p.exitValue());
int b = 0;
while((b = bis.read()) > 0)
{
System.out.print((char)b);
}
System.out.println("Called jar");
}
}
I took reference from here.
the command:
java -Xmx32m -jar yui.jar in.css
works fine in cmd but I get no output when I run above program
the output I get for above is:
Calling jar
1
Called jar
Please tell Me what I am doing wrong or what is the right way of doing this.
You are getting the "1" because there is nothing to read in the stream.
I'm guessing that you're trying to run the file from Eclipse or some other IDE. If so, you need to place your yui.jar and in.css files to same directory relative to where your Run class is.
If you're using default run configurations, you'll just want to put the files into the root directory of your eclipse project. For example, this works for me:
Test
src
com
test
Run.java
yui.jar
in.css
A better way to handle your situation is to use absolute or relative paths instead of just specifiying yui.jar or in.css. Create two variables for the two relative paths and then create the command string.

How to create a working .jar for a java project w/ excel input and System.out.print output?

I created this java project that basically gets data from an user determined excel file and uses Syste.out.println() to display the results. It works as I want it to in eclipse, however when I exported is as a .jar file, it doesn't work properly. It prompts for the excel file location to be entered, however, does not display the output, not even an error. I do not know how to do it from the terminal so I'm running it by double-clcking it. Also, I want the user to choose any excel file they want, so what should they write down as the location when prompted to do so? Right now, the excel file is in the same directory as the project. So just the name of the excel file is enough input, but what if it is not in the directory, how do i show it's location then?
Thank you
First of all, in your JAR file the file META-INF/MANIFEST.MF must contain your main class (i.e. the class with the main() method), with a line like this:
Main-Class: mypackage.MyMainClass
Make sure your settings in Eclipse are generating this line when generating the JAR file.
You can run it from the terminal with java -jar yourapp.jar however I presume that you have some extra libraries you need to include in the classpath with the -cp switch.
The working directory is normally the directory from where you are running the application. If you want to specify a different path it has to be either relative to that, or absolute.
An absolute path in windows would be something like: "C:\mydatafolder\myexcelsheet.xls"
An absolute path in Unix would be something like: "/home/myaccount/mydatafolder/myexcelsheet.xls"
You need to read System.in to get the file path, with a function like this:
private static String getFilePath () {
String filePath = null;
while (true) {
System.out.println("Please input the path of the file:");
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
filePath = br.readLine();
File file = new File(filePath);
if (!file.exists()) {
System.out.println("Sorry, invalid file path. Please try again.");
} else {
return filePath;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Sorry, unexpected error happened, Please try again.");
}
}
}

Java file input as command line argument

How do you process information in Java that was input from a file. For Example: suppose you have a file input.txt. The contents of this file is:
abcdefghizzzzjklmnop
azzbcdefghijklmnop
My hope would be that the information would be put into the argument array of strings such that the following code would output "abcdefghizzzzjklmnop"
class Test {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
The command I have been using throws an array out of bound exception. This command is:
java Test < input.txt
Non-file based arguments work fine though. ie. java Test hello,a nd java Test < input.txt hello.
More information:
I have tried putting the file contents all on one line to see if \n \r characters may be messing things up. That didn't seem to help.
Also, I can't use the bufferedreader class for this because this is for a program for school, and it has to work with my professors shell script. He went over this during class, but I didn't write it down (or I can't find it).
Any help?
You should be able to read the input data from System.in.
Here's some quick-and-dirty example code. javac Test.java; java Test < Test.java:
class Test
{
public static void main (String[] args)
{
byte[] bytes = new byte[1024];
try
{
while (System.in.available() > 0)
{
int read = System.in.read (bytes, 0, 1024);
System.out.write (bytes, 0, read);
}
} catch (Exception e)
{
e.printStackTrace ();
}
}
}
It seems any time you post a formal question about your problem, you figure it out.
Inputing a file via "< input.txt" inputs it as user input rather than as a command line argument. I realized this shortly after I explained why the bufferedreader class wouldn't work.
Turns out you have to use the buffered reader class.
I'm not sure why you want to pass the contents of a file as command line arguments unless you're doing some weird testbed.
You could write a script that would read your file, generate a temporary script in which the java command is followed by your needs.

Categories

Resources