I made a simple JOptionPane class that should pop up with a string, when I type:
javac Hellodialog.java
nothing happens at all. No error messages but nothing comes up.
import javax.swing.JOptionPane;
public class Hellodialog
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "fsdfsdfdsfds");
}
}
compile into a java .class file
javac Hellodialog.java
run the java class
java Hellodialog
You use the javac command to compile your source to byte code, and the java command to run your compiled code.
Related
When I try to run a java program in the cmd it displays the messege "Could not find or load main class".
The problem occurs when there is a package involved in the program, otherwise it works fine.
The code is:
package myPackage;
public class index {
public static void main(String [] args){
System.out.println("Hello World");
}
}
I've tried writting in cmd: javac (name of package) . Class name, but still doesnt work.
The issue was that the class path needs to be set for each command (javac and java):
Attempted Steps
Compile index.java from the top_level. do not use sub package.
$javac -cp . importpackage/subpackage/index.java
I'm a relative Java newbie so apologies if the question appears somewhat basic. I've googled high and low for an answer here and I'm not finding anything that's helping.
Problem:
Whilst I'm able to integrate external packages into my Java programs from an IDE environment, I am trying to do run a very basic program from the command line that calls on a separate, basic package file that I have written - and am simply doing all this as I want to have a bottom-up understanding of how package files are related to a main program by Java.
I have a program that sits on my desktop named MyProgram.java:
import org.somepackage;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
Still on the Desktop, I then have another folder which I've named src, inside of which I have created the necessary subfolders corresponding to the package name, i.e. ./src/org/somepackage - and in this location, I have the Human.java file which defines the Human class with the following contents:
package org.somepackage;
public class Human {
public void scream() {
System.out.println("I exist!!");
}
}
I then created a classes folder, again on the Desktop, and ran the following compile command on the command line:
javac -d ./classes/ ./src/org/packagename/Human.java
This ran fine and created - as expected - the Human.class file within the ./classes/org/packagename/ location.
However, where I fall down is when I then try to compile MyProgram.java on the command line, i.e.
javac -cp ".:./classes/" MyProgram.java
As you'll see, my class path contains a reference to the current location (".") for the MyProgram.java file, and it contains a reference to the classes folder ("./classes/") which is the base location for the org.somepackage package inside whose subfolders (./classes/org/somepackage/) on can find the Human.class file.
At this stage, I was simply expecting the java engine to compile MyProgram.java into the program MyProgram.class - but, instead, I get an error:
MyProgram.java:1: error: package org does not exist
I've been following the instructions listed here:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html
and I don't appear to be deviating from the instructions - yet I'm unable to locate an explanation on Stackoverflow or anywhere else as to a possible reason for this compile failure. If anyone has an idea, your help would be very much appreciated.
Thanks,
Your mistake is here
import org.somepackage; <--
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
you forgot to import class actually, you need to write this name
import org.somepackage.Human; import all package content import org.somepackage.*; or write full qualified name of class in your code
org.somepackage.Human myHuman = new org.somepackage.Human();
myHuman.scream();
correct mistake:
import org.somepackage.Human;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
after that compile your Human.java by this command:
javac -d classes Human.java
and MyProgram.java
javac -d classes -cp "classes" MyProgram.java
and run MyProgram by
java -cp "classes" MyProgram
I have:
public class JavaBatchInserter {
public static void main(String []args) {
System.out.println("Hello World");
}
}
I go to the pwd where my file, named JavaBatchInserter.java, is located and run:
java JavaBatchInserter.java
and get:
Could not find or load main class JavaBatchInserter.java
It's the simplest thing and I can't make it work. ): Help please! Thanks!
Note: I don't want to create a project and a bunch of directories if possible.
Firstly, compile the file JavaBatchInserter.java
javac JavaBatchInserter.java
Then give your full class name to JVM for execution
java JavaBatchInserter
You run the command java JavaBatchInserter.java
It is wrong.First compile the class by running javac JavaBatchInserter.java
The try to run by java JavaBatchInserter
I am trying to create a package of library functions for a main Java program but I am have some issues.
I don't know much about about Java packages and I am going through some documentary online.
I have created my directory as such
./Program/Program.java
./Program/TestFunc.java
./Program/classes/library/
The contents of TestFunc.java are
package library;
public class TestFunc {
public void message01() {
System.out.println("called message01");
}
public void message02() {
System.out.println("called message02");
}
}
I compiled it as I read in the documentation
javac -d ./Program/classes TestFunc.java
Which gives me
./Program/classes/library/TestFunc.class
Then I try to call it in Program.java
import library.*;
public class Program {
public static void main(String[] args) {
System.out.println("Starting Script");
}
}
When I try to compile using
javac -d ./Program/classes Program.java
I get the error
package library does not exist
Why is this?
You've used -d which says where to put the output, but you haven't told it that the same directory should also be used for input on the classpath. Use the -cp option for that:
javac -d classes -cp classes Program.java
(It's not clear whether you're trying to do this from inside the Program directory, or above it - your source filename appears to be inside the Program directory, but you're specifying the output directory as if you were in the directory above...)
package day1.examples;
public class String2 {
public static void main(String[] args) {
String x = "Andrei Vlad";
System.out.println("Hello" + x);
}
}
I keep getting this error when i run it
Error: Main method not found in class day1.examples.String2, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
and on line error it says type mysmatch cannot convert from java lang string etc..
Thank you
It works for me:
[steve#newbox ~]$ cd /tmp=
[steve#newbox tmp]$ mkdir -p day1/examples
[steve#newbox tmp]$ cat > day1/examples/String2.java
package day1.examples;
public class String2 {
public static void main(String[] args) {
String x = "Andrei Vlad";
System.out.println("Hello" + x);
}
}
[steve#newbox tmp]$ javac -classpath . day1/examples/String2.java
[steve#newbox tmp]$ java -classpath . day1.examples.String2
HelloAndrei Vlad
[steve#newbox tmp]$
The most likely explanation is that you have managed to get Eclipse rather confused.
My initial idea was that this was a homoglyph problem. But provided that you have copy-and-pasted the code correctly, the evidence disproves that.
The other idea I had was that you had mistakenly created your own version of the String class (in the day1.examples package). However, that should have resulted in compilation errors in the initialization of x.
I'd start by trying with a clean of the project. Seems weird you would need to do so with only one class in a single package but it will recompile all of your code for you. Everytime I have an issue that I don't think I should have I always F5 (refresh) my packages and then clean. You'd be surprised how many issues it fixes.
Go to Project --> Clean
Then try running again.
The code says that there are somethink wrong with your main class but all seems ok.
In Eclipse rigth click on your projects-> run-> run configurations and check if all is ok.