Java Compiler on Windows wont create executable, only .class [duplicate] - java

This question already has answers here:
How do I run a Java program from the command line on Windows?
(13 answers)
Closed 2 years ago.
Disclaimer: The question has a similar solution and arguably duplicated from "How do I run a Java program from the command line on Windows?", but I still believe it can be helpful to those whom asked the same wrong questions like me.
My mistake, as it can be found in the comments, was to be expecting the compiler to spill out an extensionless executable file (like in C), but instead, I only saw a .class file being created, leading me to run mistakenly java filename.class
If that happens, just run java filename with no extension and the code will run correctly.
I'm a very beginner at java and don't have the option of using it in Linux, so I was following the tutorial from an algorithm course on Coursera from Princeton Uni, and I installed the jdk-11.0.2 from here: https://lift.cs.princeton.edu/java/windows/
Just to see myself getting a class file instead of an executable after running javac file.java
I searched around and couldn't find anything so far, so I hope someone here saw a similar issue at some point.
The code, just to avoid suspiciousness over it:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
NB: IntelliJ on Windows compiles and runs it perfectly through the following command:
"C:\Program Files\Java\jdk-11.0.2\bin\java.exe" "-javaagent:{PATH_TO_INTELLIJ}\lib\idea_rt.jar=64104:{PATH_TO_INTELLIJ}\bin" -Dfile.encoding=UTF-8 -classpath {PATH_TO_MY_PROJECT};{PATH_TO_MY_PROJECT}\.lift\stdlib.jar;{PATH_TO_MY_PROJECT}\.lift\introcs.jar HelloWorld

A java compiler produces compiled code (.class) files.
To run it, you invoke it w/o the extension (i.e. java HelloWorld)
More details: https://beginnersbook.com/2013/05/first-java-program/

Related

Compiling and running Java code in Notepad++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I got a new computer and I'm trying to get Notepad++ to run how it did on my old one. I got the NPPExec plugin. When I click Execute, it brings up the commands I want to enter. My goal here is to have the code save, compile, and run in the console of Notepad++. Currently I have what I thought used to work on my old laptop.
cd $(CURRENT_DIRECTORY)
javac $(FILE_NAME)
java $(NAME_PART)
Whenever I try and execute it though, I get
Current directory: C:\Directory
javac HelloWorld.java
; about to start a child process: "javac HelloWorld.java"
CreateProcess() failed with error code 2:
The system cannot find the file specified.
java HelloWorld
; about to start a child process: "java HelloWorld"
CreateProcess() failed with error code 2:
The system cannot find the file specified.
I've tried it on files I used to have, and just a basic Hello World
class HelloWorld
{
Public static void main(String args[])
{
System.out.println("Hello World");
}
}
I don't get what the problem is, and would love if someone would be able to help.
Edit: I am using Windows
After checking here: https://beginnersbook.com/2013/05/first-java-program/ and seeing the Set Path section, I reinstalled Java, did the set path command, although the file path ended at \Java and did not have that whole JDK bit in it. I'm not sure if that's part of the issue and Java is not installed properly.
Edit 2: Tried to Find where I can install JDK, and everything sent me to Oracle's website. Is this needed?
Edit 3: Well, I tried just executing the javac command through command prompt and got
'javac' is not recognized as an internal or external command, operable program or batch file.
So I'm pretty sure it's the issue with JDK not being in the folder. I went to check my old laptop, and it didn't have JDK in the folder either. It gives the same error when using the javac command, but for some reason the java command works fine to execute the file. Do I really have to create an Oracle account just to get JDK? When I went to sign up it wanted my work phone number, so it definitely didn't seem like a free thing.
Edit 4: I tried following the Oracle documentation on installing a jdk. I did the download from there, but following that, I didn't see the file I needed. In the Java folder, there is now the jdk-16 folder. In the bin folder there, there are a number of .exe files, but I don't know if any of them are the ones I'm supposed to run. Now that I've installed it though, I'm still getting an error with javac command, but the java command will run files I'd previously compiled.

Why do I get an exception when trying to run .class file made with Jython? How can I solve it? [duplicate]

This question already has an answer here:
Issues understanding how to use Jython
(1 answer)
Closed 4 years ago.
I just found out about Jython and I really wanted to use it. Please forgive me if I'm doing something wrong and please explain.
I made a simple python program to test Jython, test.py. When typing
jython test.py
in the command prompt it run normally. So I decided to make a Java .class file. Since jythonc doesn't exist anymore, I tried
jython -m py_compile test.py
and a .class file was created in the same directory called test$py.class.
(this is equivalent to
>>> import py_compile
>>> py_compile.compile('test.py')
as I tried as well)
However, when I tried
java test$py
it threw me an exception:
Error: Could not find or load main class test$py
Caused by: java.lang.NoClassDefFoundError: org/python/core/PyRunnable
(I also tried with classpath but it did the same exception)
What does it mean? How can I solve it?
Note: I've downloaded Jython 2.5.4rc1.
This could be a corrupted installation of jython as per simillar Linux: NoClassDefFoundError org/python/core/PyException #1006 issue. If you can find jython-standalone-2.5.3.jar and it's not corrupted you may want to try re-downloading and re-installing jython.

Able to compile java files, unable to run files [duplicate]

This question already has answers here:
Error: Could not find or load main class [duplicate]
(22 answers)
Closed 5 years ago.
I am trying to run these programs, but I am getting
Error: "Could not find or load main class"
Here's a screenshot of me trying to run the programs in cmd line:
Windows Powershell Screenshot:
This makes no sense to me seeing as how the files compiled just fine, which would imply that the main class was able to be found.
If anyone could explain what's going wrong I would appreciate it very much, thank you.
The UDPServer code:
The UDPClient code:
You have defined a package serverClient; at the top of both files.
So you should be having a directory named serverClient with your .class files.
if you wish to execute using java command line, you should execute from the src directory like this
PS ...\Programming Assignments\src > java serverClient.UDPServer
PS ...\Programming Assignments\src > java serverClient.UDPClient
It would be useful if you post your code here. But if I try a shot in the dark I would say that your filename doesn't match the classname in your Java file.
When running javac you pass in the paths to files you want to compile - hence those files are implicitly on the classpath. When running java you aren't passing in any files explicitly, so you have to include the current directory in the classpath in order for the JVM to know to look there.
$ javac Foo.java
$ java -cp . Foo
I use this Bash function pretty often for quick JVM/JDK experiments, if you want to try to replicate it in Powershell.
It's one of those cases where the current working directory probably should be on the classpath by default ~90% of the time, but it can't be in order to accommodate that last 10%. (Whether that's a good design decision or not is debatable, of course).
You are:
In the wrong directory. You should be in the directory that contains serverClient.
Using the wrong commands. They should be:
javac serverClient/*.java
java serverClient.UDPServer
java serverClient.UDPClient

Once again: "javac: file not found: hello.java [duplicate]

This question already has answers here:
Why this javac: File Not Found error?
(5 answers)
Closed 8 years ago.
I was using Java SE Jdk1.7.0_40 and it was working perfectly fine. I've no idea what went wrong, I've set the path many times through different ways. If I check in cmd like by writing javac or java -version, it results affirmative but when i write
javac hello.java i get this error..
javac: file not found: hello.java
Usage: javac
use -help for a list of possible options
I went through almost all of topics related to my problem but all are saying the same thing that is "to set the path, may be your path is not correct, etc etc". I've re-install java but still no use.
Is there any step by step procedure to check whether the path is actually responding or not?
Am using Windows 7 and current version of Java is SE jdk1.7.0_60
a thing more.. when I compile the java file from the folder containing that file, it works fine and generate .class file but when I execute the class file ..
java hello , i get this
Error: Could not find or load main class hello
may be i'm missing something..
Help...
There is no problem with javac not being on your PATH, since the error you see is an error from javac, which could only occur if javacis on your path and running.
The only possibility is that hello.java is not in the current working directory from which you are invoking the command. Putting the directory that hello.java lives in onto your PATH will not help here.

compiling java programs [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
javac not recognized
I'm starting in the world of programming in java and I have run across a big problem: i cant compile my programs. when I type the javac command in cmd.exe(im using Windows 7) it displays this:
'javac' is not recognized as an internal or external command, operable program or batch file.
So I can't even test my first program.
I found in a book the following command:
set path=%path%;c:\java\jdk1.6.0\bin
but it doesnt work either.
I downloaded the newest version of the Jdk package just 2 weeks ago.
Look through your C drive for the c:\java folder and make sure you have version 1.6.0 of the jdk
c:\java\jdk1.6.0\bin
This will only work if you give it a path that actually exists. If you don't have 1.6.0, then type in:
set path=%path%;c:\java\<YOUR_ACTUAL_JAVA_VERSION_DIRECTORY>\bin

Categories

Resources