I'm trying to compile my programs using the command prompt on Windows 7. I'm having a problem when I during compile. I created a test program:
class test
{
public static void main (String args[])
{
System.out.println("this is working!!!!!!");
}
}
I use the following instructions from the command line:
cd \
cd summer
Javac test.java
java test
I have even tried using javaw and I get the same message but in a pop-up box
Could not find the main class, program will exit.
Any suggestions?
It seems your JDK and JVM have different editions.
You are using different versions of JDK. Check the versions on your javac vs java. This error is telling you that your java and compiled class are not same versions. Check your path for configuration, type "set" in dos to see details.
Example: If you compiled your class with javac (version 7) and execute it with java (version 6)
Related
I wanted to check out some new features of java 11 which was released two days ago.
JEP 330 states that I could launch a Java-Source-Code-Program without compiling.
It should also support the usage of Shebang-Files.
Hence I have written this small Hello-World Program Test.java:
#!/opt/java/jdk-11/bin/java --source 11
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
I downloaded JDK 11 and extracted it to /opt/java.
Hence the Shebang itself is working.
I.e. executing /opt/java/jdk-11/bin/java --version gives me
openjdk 11 2018-09-25
OpenJDK Runtime Environment 18.9 (build 11+28)
OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)
After making Test.java executable (using chmod +x Test.java) the execution is failing.
I.e. ./Test.java gives me:
./Test.java:1: error: illegal character: '#'
#!/opt/java/jdk-11/bin/java --source 11
^
./Test.java:1: error: class, interface, or enum expected
#!/opt/java/jdk-11/bin/java --source 11
^
2 errors
error: compilation failed
As soon as I remove the Shebang-Line from Test.java and start it with /opt/java/jdk-11/bin/java --source 11 Test.java
everything is working like a charm and I get the expected output: Hello World!
My machine is running Ubuntu 17.04.
I have linked javac to the one from JDK 11 (i.e. executing javac -version gives javac 11).
The file name must not end with .java in order for the java executable to ignore the shebang line. You can use a different extension, or just have no extension at all (which is what they do in the JEP example and is what I would recommend).
From JEP 330 (emphasis added):
When the launcher reads the source file, if the file is not a Java source file (i.e. it is not a file whose name ends with .java) and if the first line begins with #!, then the contents of that line up to but not including the first newline are ignored when determining the source code to be passed to the compiler. The content of the file that appears after the first line must consist of a valid CompilationUnit as defined by ยง7.3 in the edition of the Java Language Specification that is appropriate to the version of the platform given in the --source option, if present, or the version of the platform being used to run the program if the --source option is not present.
It doesn't need to end with ".sh" specifically; also, that's potentially misleading because the file is not actually a shell script.
A bit of trial and error gave me the correct solution.
It was the file extension .java which causes those problems.
I.e. if I rename the file to Test.sh everything is working.
Here is a complete Hello-World-Shebang-Example:
Create a File Test.sh with content like
#!/opt/java/jdk-11/bin/java --source 11
public class Test
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Make it executable (i.e. chmod +x Test.sh).
Last but not least execute it using ./Test.sh
According to the JEP you've linked to (see the shebang files section), the shebang file is to be used to launch the java process, not to be used as a parameter for java:
A shebang file to invoke the Java launcher using source-file mode must begin with something like:
#!/path/to/java --source version
For example, we could take the source code for a "Hello World" program, and put it in a file called hello, after an initial line of #!/path/to/java --source 10, and then mark the file as executable. Then, if the file is in the current directory, we could execute it with:
$ ./hello
In other words, what you want to do is rather make Test.java executable. You'd also have to rename it since it won't work as shebang and strip first line when it's named *.java.
$ move Test.java test
$ chmod +x test
$ ./test
This will launch shebang processor which will strip first line and pass the rest of the script to /path/to/java and Java will compile the script and run the main method.
I've been trying to get back into programming and I've been redoing some old labs. I'm setting up Textpad 8 so I can run java applications and it works fine until I add a package statement like so:
package pkg;
public class inPkg{
public static void main(String args[]){
System.out.println("Hello World");
}
}
The file's location: C:\214\pkg\inPkg.java
When I compile everything is fine but when I try to run it, I get this error message:
Error: Could not find or load main class inPkg
Tool completed with exit code 1
Compile Java Tool:
Parameters: javac -classpath "$FileDir;h:\214\;c:\214\;" $File
Run Java Application Tool:
Parameters: java -classpath "$FileDir;h:\214\;c:\214\;" $BaseName
These tools are the only thing I changed in the configuration. The classpath have been written to follow the lab. instructions.
PS. Without the packages statement, the application runs perfectly.
Because probably you are not using the fully qualified class name when you do the java run. use
java -classpath 'your class path' pkg.inPkg
It will compile and execute correctly with following commands
C:\214>javac.exe pkg\inPkg.java
C:\214>java.exe pkg.inPkg
Note that the file location is C:\214\pkg\inPkg.java, however you execute the commands from C:\214
I have downloaded whole java 3 times and still get this error after executing in cmd
enter image description here
Make sure to compile code with exactly the same version of Java you are trying to use.
To do so, make sure that both:
java -version
javac -version
produce the same version information.
Let's say you have file: Hello.java
public class Hello {
public static void main(String [] arg) {
System.out.println("Hello world!\n");
}
}
Make sure to compile it:
java Hello.java
and then, run it
javac Hello
Your %PATH% variable should point to very same JDK and JRE if you want to compile and run the code.
in cmd, type where java, remove all other java path from environment variable other than the one from jdk directory
I have a set of instructions to create a Java application that takes multiple arguments when running the application from a CMD line.
The instructions state:
Thus, using the example set of above, assuming the main() method of your program is in a class called JavaClassName, the output should be:
$ java JavaClassName 4 7 file.csv
program output here
My question is:
Isn't this skipping the compile process?
Would they assume that loading the Java classes onto a computer that has never run this application before (or a directory with purely the .java files needed to run); running the cmd
$ java JavaClassName 4 7 file.csv
would output something?
Sidenote: Currently, running that CMD outputs
Error: Could not find or load main class JavaClassName
Have ran through multiple SO questions and online tutorials trying to get this to even run but I have yet to get it to work.
You ask:
Isn't this skipping the compile process?
Absolutely yes. A command line like java JavaClassName 4 7 file.csv assumes that there is a compiled class file "JavaClassName.class" in the current directory (or in some other directory or Zip/Jar file found in the CLASSPATH environment variable). And yes, to produce that "JavaClassName.class" class file, you have to use a java compiler first.
from Java 10 it is possible to run java programs that fit a single file without manually run the compiler first.
It will be compiled on the fly before execution. Nice and useful for scripting.
e.g. writing HelloWorld.java file
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
we can run it as
java HelloWorld.java
We can also add more classes in the single file.
from Java 11 we can also use shebang files
we have to specify which version of the java language you want to use
we have to save the file without .java extension
remember to give executable permissions chmod +x HelloWorld
writing HelloWorld file
#!/path/to/java --source 11
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
and we can run it as
./HelloWorld
I'm getting started with java development. So, I installed JRE and JDK in my computer.
Then, I created a simple Example.java file and saved it in my Desktop.
In the prompt, I executed
javac Example.java
and it worked ok. A .class file has been created in my Desktop.
Then, I tried to run the class, executing this:
java Example
and I got an error in a window alert, with this message:
Java Installation Not Completed
Unable to install Java
There are error in the command line switches: "Example";.
Check that the commands are valid and try again
Then, for testing, I executed both commands:
javac -version and
java -version. Both are installed in my computer.
What am I doing wrong?
I am running Windows 8 and have already set my environment variables.
Example.java:
public class Example {
public static void main(String args[]) {
System.out.println("Finally Java");
}
}
Try to remove installation again, look for all leftovers and remove them manually, if you changed the directory you installed java to, remove environment variables as well and set them again. You should also make registry cleanup: https://java.com/en/download/help/manual_regedit.xml Make installation through offline installer.