This question already has answers here:
How to run a Java project in command line
(6 answers)
Closed 7 years ago.
I'm using a mac, and I'm trying to run a java project created in Eclipse from the command line. My project folder has a bin and src folder, each of which have folders game and display corresponding to packages. There are .java files in src/*/ and .class files in bin/*/
Here's what worked:
I compiled everything using javac src/*/*.java. I ran my project by going to the src folder (cd src) and using java game.Gomoku following the directions in this answer. Everything worked. The only problem was that now I had a bunch of .class files in my src folder, and I wanted them in my bin folder like Eclipse organized it.
The problem:
I figured out that I could do this with javac -d bin src/*/*.java. But then when I try java bin/game/Gomoku or java game/Gomoku or java -cp bin bin/game/Gomoku I get the error Error: Could not find or load main class bin.game.Gomoku. How can I run my project?
So while I was typing my question I came across a solution. After going to bin (cd bin) I could run my project with java game.Gomoku. I also found that my question had been previously answered here, but I'll post this anyways just in case my method for compiling (in my question) can help anyone.
Related
This question already has answers here:
Connect Java to a MySQL database
(14 answers)
What is a classpath and how do I set it?
(10 answers)
Visual Studio Code, Java Extension, how to add a JAR to classpath?
(10 answers)
Closed 10 months ago.
I'm working on a lab for a class where the teacher just said
Go here, install this, do the lab. "https://dev.mysql.com/downloads/connector/j/"
Now I followed that, installed mysql, ran a server, tested it with workbench, and a few queries. Works.
However the rest of the lab was in java, so I built a folder in VSCode, lab x, ran javac *.java, java Program
The error I got was "Client exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver"
Now this makes no sense since I installed mysql with the JConnector, and added it to my computer's environmental variables, I looked online and all I'm finding is stuff about Maven and Netbeans, I'm not familiar with either. I just want to complete this lab.
I tried adding it to the folder, then saying "javac -cp mysql-connector-java-8.0.28.jar Program.java" but then any java file referenced within the same folder was a symbol it couldn't find.
BAD ANSWER - Old
I figured it out.
I opened the jar file in 7zip, dragged out the com folder and put it in my lab folder.
Works!!
GOOD ANSWER - VSCode - New
Download Java Extension for VSCode.
Go to Command Pallet (ctrl+shift+P.) and write Create Java
Project.
Click on Build Tools
Name it whatever your project's name is.
Drag/Drop your Java files into the folder "src" in the project's folder.
Look at the bottom left of your screen where it says Java Projects and expand it.
Hover on the right side of Referenced Libraries and click the + button.
In the popped up Open window find where you have Connector/J and select the .jar file (in this case, mysql-connector-java-8.0.28.jar) and click Select Jar
Libraries
Click the Refresh button on your Java Project and click Build Workspace and pick Full
Restart VSCode
Click the Run and Debug button / Press ctrl+shift+D
Works!
GOOD ANSWER - Command Line - New
javac -classpath "[Project folder or just a "." if your console is in it];[folder with jarfile]\\[jarfile].jar" Program.java
java -classpath "[Project folder or just a "." if your console is in it] ; [folder with jarfile]\\[jarfile].jar" Program
In this specific case
javac -classpath ".;C:\\Program Files\\MySQL\\Connector J 8.0\\mysql-connector-java-8.0.28.jar" Program.java
java -classpath ".;C:\\Program Files\\MySQL\\Connector J 8.0\\mysql-connector-java-8.0.28.jar" Program
So in the background the VSCode Answer does what the Command Line Answer does.
Basically every time you run java or javac, java adds a thing implicitly pointing/referencing where your classes (compiled .java programs) are (in a folder, or a zipped folder called a .jar), to your java/javac commands called a classpath in between the java/javac and your .java file.
With the implicit
javac Program.java
java Program
Being equivalent to
javac -classpath "." Program.java
java -classpath "." Program
The implicity makes sense as you would be using the classes from the folder that the program is in. (Note: The "." means the folder the Commandline is in, navigated by cd/ChangeDirectory in Bash/PowerShell, see Linux/Mac/Unix Path Formats and/or Windows Path Formats ).
Now you might be wondering why there's a ";", this is to denote multiple classpaths, as you want to look into your projects, ".", and the library JDBC, if you wanted to add more classpaths add another ";", however on Linux/Mac/Unix you would add ":" instead of ";".
The reason the old Bad Answer worked was because the dragged folder was included in the implicit classpath.
The reason the old Bad Answer is a bad answer is because its reflective of poor practices and lack of understanding. Its poor practice for few reasons: the zipped files take up less space thus unzipping needlessly eats space, these files aren't yours so mixing them with yours could violate the terms of use of the author, and its a lot of work unzipping that you're doing for no reason.
This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 6 years ago.
I am learning Java, and I have been programming an 80s-styled text based game. I have experience with coding so I know I will be done soon. Just so I knew how, I decided to make a .jar file. It created fine, but I have a problem: I want it to run and work in the command prompt, just like if I were using the "java FileName" command. Is there any way to do this? I want to distribute it so that for Windows, it runs in the command prompt exclusively (Apple and Linux is down the line).
Edit/Summary: To some people, this might be a duplicate of a question. It's not for these simple reasons: I know how to create a .jar file. I am trying to figure out how to create a specific type of .jar file that opens and runs in the command prompt.
I assume that you have compiled your .java files into class files. If so, than do the following:
Make a manifest file called manifest.txt and inside the file mention the class name where the main class resides. For example:
Main-Class: com.A.B.MyMainClass
com.A.B.MyMainClass is example here. You should mention your package names.
Put the above line in the manifest.txt file
Than, suppose you have your class files in c:\test\classes\ folder and suppose your class files in this folder are a.class and b.class and MyMainClass.class
Now execute the command:
jar -cvfm MyExecutable.jar manifest.txt c:/test/classes/*.class
It will create an exectable jar for you which you can run in Windows and Mac and also in Linux.
To execute the jar file:
java -jar MyExecutable.jar in the command prompt.
Use this to run your jar on command line:
java -jar <jar-name>.jar
Assuming that you have java installed on the system
Using eclipse -
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm
Using Command line -
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
To run the jar file -
http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
This question already has answers here:
How to run a NetBeans project in command prompt?
(9 answers)
Closed 6 years ago.
Im trying to run a javafx project file by using the cmd in another computer that does not have netbeans on it.
Any suggestion on how to run my project file without installing netbeans (the source file is rather complicated)
And what does a batch-file means?
With the jar file you can do either
java -jar path/to/jar/file
if the jar file has a manifest with a Main-Class entry, or
java -cp path/to/jar/file MainClass
where MainClass is the fully-qualified name of your Application subclass.
This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 9 years ago.
I have a directory in which I generated *.class files. How can I build executable jar file from this files.
What I need to know is
how to clean dir
how to put all files in jar and make it executable
Sorry people i didnt mentioned, i need do this programmatically in java code.
java cfe myJar.jar Classname.class myClass
and for exceution
java -jar Jarname.jar
Creating a Jar
If you're using an IDE such as Eclipse or Netbeans, there are specific easier ways to do it. If not I think the following command would work:
jar cfe <jar-file> <main-class-name> <class-files...>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Corrupt jar file
I made a game in Eclipse and packaged all the class files in test.jar. When I attempt to run it, it gives me the error:
Error: invalid or corrupted jar file.
I opened the .jar file using Winrar, and everything is in there.
I have looked for a similar question on Stack OverFlow, but didn't find any. Can you explain to me how I can solve this error?
FOR EVERYONE WITH THIS PROBLEM ,SOLUTION:
Go to file, export, export as a Runnable Jar, Select your main class, finish.
If you want to run your program in cmd, create a new textfile, and type java -jar project.jar with project = name of your program.
Save this as Run.bat
then open the Run.bat, it will automatically open project.jar
Thats it, it worked for me!
How you place your classes to tar.jar? A valid jar file must follow some rules it requires. You'd best generate them by your Eclipse tools. click "export" -> "jar" and select the right entry of your game. Good Luck.