How to make Eclipse run my program in a terminal - java

Sometimes, the console in Eclipse just isn't enough, or my program has to be run in a terminal of some sort. How would I achieve this?
I'm using Eclipse Mars 4.5.0 on a MacBook.
Edit:
I know how to run Java programs from a terminal, but what I want is Eclipse to do this automatically when I press the run button.

Making Eclipse do it for you isn't possible
To do it manually:
First, go to the directory with your *.java file. To go to the directory, use the cd command. Then, run javac yourfile.java. Lastly, java yourfile

Related

Eclipse Executable from terminal

Hey I downloaded the latest version of eclipse and extracted the folder and ran it using ./eclipse everything worked fine. I was wondering if there is a way to just run eclipse or eclipse & from the terminal and eclipse will execute without going to the eclipse directory. I did it before when I used sudo apt-get to install eclipse but they don't have the latest version of eclipse. I am using linux btw and I have to navigate to the folder where I put eclipse every time I want to run it. If there is a obvious answer to the question I apologize for asking but I couldn't find anything on it.
Thank you for any input.
Man, is normal that way to execute eclipse, eclipse does not install, is like a executable. You need use ./eclipse always and its normal you need go to directory.
Why dont create a desktop launcer like here
If you are using bash there is a file called .bashrc, enter the following commands:
$ cd ~
$ ls -a
If you look, you should see .bashrc, then you need to edit it using some text editor like nano:
$ nano .bashrc
Once inside nano, you need to create an alias. An alias is a global variable in bash that references another value, for instance:
alias eclipse="/path/to/executable/eclipse"
This is create a variable called eclipse that when typed into the terminal, in any directory, will execute and run eclipse. So simply add that line, replacing /path/to/executable/eclipse with the actual path of the program.
Once that is done you can type eclipse anywhere in bash and it will work.
SIDE NOTE:
If you don't want your terminal session to hang when you open eclipse, type an ampersand after eclipse:
$ eclipse &
This will run the program in the background, freeing up your terminal.
You can edit your .bashrc file and modify the PATH environment variable to include the path where you installed Eclipse.
Also, you should consider adding a desktop launcher which you can just double-click to run Eclipse.

Running jar file in command prompt. Java can't find libraries in classpath

EDIT: OK, I think I was being silly. What I really wanted to do was a runnable JAR. I did and now it works.
So I'm trying to run a little program that interacts with some webpages. The program works fine when I launch it from Eclipse but when I export it to a jar file and try to run it from the command prompt I get this error message:
What am I missing here?
I'm assuming in Eclipse you've added the appropriate libraries to your build path. When you run your program through eclipse, it automatically creates a java command including all your libraries in the classpath.
You need to do the same thing
java -cp /path/to/libs -jar muzictest.jar

java: when running jar file from icon Runtime.getRuntime.exec() doesn't work

so I have this problem. I have an aplication, which is using Runtime.getRuntime.exec(adobePath) to run Adobe Reader.
When I run my aplication using NetBeans or from cmd line by using java -jar "MyApp.jar" it does work fine. But when I run jar file by double-click or from cmd using MaApp.jar it doesn't work. I'm using Windows 7.
What can be wrong?
Thanks
edit: Maybe I said it wrong way. When I run jar file by double-click it does run. And it works, but when Runtime.getRuntime.exec(adobePath) is called nothing happens. And It work fine in NetBeans or when I run App by java -jar "MyApp.jar".
Right-click on the jar and see under Open With ?
Make Java Runtime as default program to run with.

how to run java program outside eclipse on a server that doesn't have eclipse

I wrote a java program that contains many classes in eclipse, it is my first time that I write a code in java and in eclipse too, now I need to run this code on a server that doesn't have eclipse.
Compiling & Running a Simple Program.
This is your best starting point.
Use "File -> Export -> JAR file" wizard to create a .jar file contained your compiled classes. You can then copy this .jar file to your server and use java.exe to run it. Use "java -help" from a command line to get instructions.
Please go through link provided by Andrew
Eclipse is an IDE , it facilitates development but not actually required to run your code, if you want to run your java file in a server somewhere, make sure you have JRE installed in your "server" and in your Path, then run following command in your server's shell environment
javac filename.java
above command will generate a .class file in the same directory, which is actual file that can be executed using following command,
java filename
assuming your code is inside a file called filename.java

Eclipse Project Compiling, Or Not?

I noticed that when I build even a very simple Java program in Eclipse, and I try to run it from the Terminal/Command Line and it gives me errors. I noticed after some hunting around that I have to actually compile the .java file I created in Eclipse in the Terminal to create and run the application. However, I can just save and run in Eclipse and get the same output (within eclipse).
I checked to see if I could build the project in Eclipse but the option to do so is greyed out. So, how can Eclipse run it if it actually never gets compiled?
Eclipse does compile it, otherwise it could not run it :-).
Eclipse generates normal .class files, just like javac. It puts them into its "build directory", which you set in the "build path" (or something - Eclipse not handy right now) dialog. By default its under /bin, I believe.
In principle, you can run your program in a terminal using these class files; you just need to set your CLASSPATH accordingly.
In practice, you would either run your program from inside Eclipse (which is for example easier to debug), or deploy your program (using e.g. Ant) to get some kind of installer or installation file, then install that and run it. That way you always run from a complete, correct installation.
Of course for small/simple programs, just running from Eclipse's class files is quite ok.

Categories