I keep getting this message in BlueJ/Java.
http://cache.gyazo.com/19c325e77bbc120892d1035dcfda5377.png
I know there are several other questions like this already on StackOverflow, but none of the answers were specific enough for me, a Java noob. For example, one of them said to add something on the javac command line," and I have no idea what is that. So use that information to know how specific you must be with me. Sorry. Thanks!
open up command prompt in the directory with your source files. then type javac -Xlint:unchecked *.java
Please refer to the BlueJ FAQ which has the exact answer.
Edit:
Sorry for the runaround. This is what the FAQ says for Windows. Go to your Bluej installation directory and open lib\bluej.defs file. Then go to the section that says bluej.windows.vm.args and add the value the other user said.
So you have:
bluej.windows.vm.args=-Xlint:unchecked
I would listen to paulsm4's advice and start learning from command-line if you really want to understand java. This is the best I can do.
#viggom555:
Here is a complete command line example:
1) Create the file "ATest.java" (EXAMPLE: notepad ATest.java):
import java.util.*;
public class ATest {
public static void main (String[] args) {
ArrayList<String> test = new ArrayList<String>();
System.out.println ("My array has " + test.size() + " items");
test.add ("abc");
System.out.println ("My array has " + test.size() + " items");
}
}
2) Compile (EXAMPLE: "javac -Xlint:unchecked ATest.java"; you don't really need the "XLint" in this example; I'm just showing you where it would go if you wanted):
C:\temp>javac -Xlint:unchecked ATest.java
3) Run the test program:
C:\temp>java ATest
My array has 0 items
My array has 1 items
I hope that helps .. PSM
Related
This question already has answers here:
Eclipse command line arguments
(4 answers)
Closed 3 years ago.
Trying to run a program that swaps names. The task is simple: input > Alice Bob Alex, output > Alex Bob Alice
P.s. Maybe the problem is stupid, but I just recently started programming, so I don't know what to do
I try to run the code in Eclipse - gives an index error. I start in the console - gives an error of the main name. Tried to pass through the internal debugger in Eclipse - writes that I am using obsolete methods. In the end, nothing is clear./
public class Noob {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print(args[2]);
System.out.print(" " + args[1]);
System.out.println(" " + args[0]);
}
}
Error message from Eclipse:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 0
at noob/noob.Noob.main(Noob.java:7)
Console (the file name is Noob.java)
First I compiled src file (> javac Noob.java)
Then I ran it (> java Noob)
Error: Could not find or load main class Noob
Caused by: java.lang.NoClassDefFoundError: noob/Noob (wrong name: Noob)
You need to pass three arguments
This is the steps you need to follow, to passing arguments
1-) Click on Run -> Run Configurations
2-) Click on Arguments tab
3-) In Program Arguments section , Enter your arguments.
4-) Click Apply
You are not passing correctly the values to your program. If you call the program from command line with java myProgram Alice Bob Alex your code should work.
Moreover, you can pass the values to your code through Eclipse directly inside Run Configuration option.
Also, it would be better to not hard code the values. Try this:
for (int i=args.length-1; i>=0; i--) {
System.out.print(args[i] + ' ');
}
System.out.println("");
this is my first time posting here and would like how to solve this error message. It appears only sometimes and only lets me build on a program called Main.java. I'm a begginer programmer so please bear with me, this is the code im trying to run:
import java.util.Scanner;
import java.text.NumberFormat;
public class Main {
public static void main(String[] args) {
Scanner priceScanner = new Scanner(System.in);
System.out.print("Price: ");
int price = priceScanner.nextInt();
Scanner interestScanner = new Scanner(System.in);
System.out.print("Interest rate: ");
double interest = interestScanner.nextDouble();
Scanner numberOfPaymentsScanner = new Scanner(System.in);
System.out.print("Number of payments: ");
int numberOfPayments = numberOfPaymentsScanner.nextInt();
Double monthlyInterest = interest / 1200;
Double result = ((double)price * ((interest * Math.pow((1 +
interest), (double)numberOfPayments))/((Math.pow((1 + interest),
(double)numberOfPayments)) - 1)));
NumberFormat currency = NumberFormat.getCurrencyInstance();
String mortgage = currency.format(result);
System.out.println("Your mortgage is: " + mortgage);
}
}
I haven't seen any comprehensible ways to solve this problem online, and the only thing i think could solve it is to reinstall java in another drive and change the classpath.
Thanks for your attention.
I solved it - my mistake. While executing the program using the terminal I was typing java Main.java, whereas the correct execution method was to type java Main.
With Single-file source-code programs which is a new way of executing 1 File Java programs is only available since Java 11. You can run the command:
java (Java File Name without .java extension)
java Main.java
Though please take into consideration that this way of executing only works if your Java project is only 1 Java File.
FYI:
This single-file source code will be executed fully in memory and you can only import code that came with the JDK you are working.
Finally if you want your code to run as fast as possible compile with javac before executing you program.
javac Main.java
java Main
Just be careful that there is no Main.class already in the folder, this may cause a confusion to the compiler.
Step 1:
javac + Filename.java
Step 2:
java + Filename // execute without add .java
I am working my way through "Java: A Beginner's Guide", Sixth Edition and I've encountered an area where I'm typing precisely what the book says, but I'm getting a undesired output.
Here's my class:
// Display all command-line information
public class CLDemo {
public static void main(String args[]) {
System.out.println("There are " + args.length + " command-line arguments");
System.out.println("They are: ");
for(int i = 0; i < args.length; i++)
System.out.println("arg[" + i + "]: " + args[i]);
}
}
My console output:
There are 0 command-line arguments
They are:
Desired console output:
There are 3 command-line arguments
There are:
arg[0]: one
arg[1]: two
arg[2]: three
I'm using Eclipse IDE for Java Developers
Version: Kepler Service Release 1
Build id: 20130919-0819
Any thoughts re: why the number of arguments from my code don't match the book's count of arguments would be greatly appreciated.
Update:
The solution to my issue turned out to be very simple. I had been running the sample projects in Eclipse by pressing the "Run" button without specifying arguments, as I've done for the prior 164 pages of the book without issue. The book instructed me to execute the program from the command line as follows:
java CLDemo one two three // where one two three are the arguments passed
Thank you to those who steered me to the solution.
It seems you are running the program from Eclipse where by default zero arguments got passed.
If you want to pass your arguments, you can do it through run -- >run configuration --> arguments tab.
Guide
Wondering if there is anyway I can clean the output of intellij when executing a java app with arguments. I require to see only the arguments and all the information before the app name have no use for me and hoping I can turn them off for now.
e.g.:
"C:\Program Files\Java\jdk1.7.0_11\bin\java" -Didea.launcher.port=7538 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_11\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_11\jre\lib\ext\zipfs.jar;C:\Users\asd\IdeaProjects\Assignment1\out\production\Assignment1;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Problem2 25 -5 10 5frf
As you can see, the arguments are the last 4 words in this. I would just simply like to see the app.java arg1 arg2 ...
Despite that I don't think you can change this, what about this simple Java solution:
class Something{
public static void main(String[] args){
for (String arg : args){
System.out.println(arg);
}
System.out.println("--------");
}
}
Seems I'm right. Here is the documentation for the "Run"-View. It doesn't include any information on doing something like this.
An idea to not forget to remove this in final releases would be to add a //TODO Remove this. IntelliJ can list all ToDo's in a list and before you ship it out you just look over all todos and remove it.
Another idea would be emulating a #define from C by simply having a public static final boolean RELEASED which you set to true when you release the code. You can check for that flag and modern Java compilers will most likely optimize the output-block away.
I have instruction to run program in command line, for example:
java SetTest < alice30.txt
I wonder how to do this in Eclipse. I tried to put this in Run Configuration like this:
Another thing I don't know is where to put this file (alice30.txt). Is this in root of project or in src folder where source files are located?
I know these are beginner questions but I am stuck and need help.
EDIT:
As #Kane suggested I passed File and opened stream.
Instead of:
Scanner in = new Scanner(System.in);
I now use:
Scanner in = new Scanner(new File("alice30.txt"));
You can pass full file path in arguments (e.g. c:/.../alice30.txt))
The eclipse root directory is the base directory of the project (i.e., not the src/ directory, directly under the project.)
It's generally good style to have a 'resources' folder for txt, graphics, etc.
Rather than trying to pass a stream you could just pass the filename and open the stream yourself.
The reason what you're doing in Eclipse isn't working is because your command prompt/shell/dos/bash/whatever is handling creating the input stream out of the file for you. Eclipse doesn't do this. So, from the command line: < alice.txt means "run this program with no arguments, and create a stream to system.in", while doing that in Eclipse means "run this program with two arguments '<' and 'alice.txt'
you need do like this:
add:
import java.io.IOException;
import java.nio.file.Paths;
then:
replace"Scanner in = new Scanner(System.in);"to"Scanner in =new Scanner(Paths.get("alice30.txt"));" .
and you also need do this : "public static void main(String args[]) throws IOException "
With information from this link/page and several tries, I figure out a way to pass argument and file using the local route in eclipse Run -> Run Configurations.. , though it is not recommended as Kane said.
For my case: I need to do " $java someClass tinyW.txt < tinyT.txt " (This is an example from Algorithms book by Robert Sedgewick)
In my case, " tinyW.txt " is a argument, so in the eclipse environment, you can set in Run -> Run Configurations -> Arguments -> Program arguments: /local address/tinyW.txt. For my Ubuntu: /home/****/tinyW.txt
" < tinyT.txt " is a file that pipe to the main arguments, so you can set the route and file in " Run -> RUn Configurations -> Common ", click the "Input File", use the File System icon and select the file from local compute. See the figure. So in Input File: /local_address/tinyT.txt. My case is: /home/***/tinyT.txt. Hope it also works for you.