Changing System.in in eclipse to read from a file - java

This seems like kind of a silly question but is there a way to change the standard in in eclipse. If I was doing this from the windows console it would look like "java myprogram < stdin.txt"

I am using Eclipse 3.4 and I see a place to enter a file for stdin/stdout on the Common tab of a Java Application Run configuration. I am not exactly sure if it will work for stdin, but it is worth a try.

The way you do this in VS is to set the command line arguments when debugging to "< stdin.txt". Maybe the same will work in Eclipse? I'd check but I don't have it on this machine.

There is a (fairly ugly) solution, as pointed out in this comment
Since the mailing list archive I originally linked to seems to have disappeared, here is a copy of what was there (in turn copied from here):
The ability to redirect standard input to come from a file is OS dependent (actually it's a function of the OS's shell, in this case your DOS window). Eclipse doesn't directly support doing what you want, but achieving this is relatively simple to do.
In your project, right click and select New->File and name it streamit.bat. Select the new file, right click and Open With->Text editor. Enter the following line and save the file:
java %1 < %2
Now create a External tool launch configuration - from the Run menu go to Run->External tools->External tools... Select Program and hit New, then in the Location field, hit Browse workspace and select streamit.bat from your project. In the Working directory field, hit Browse workspace and select your project. In the Arguments field enter:
StreamClass input.txt
(this assumes input.txt is located in your project) Now hit Run. It'll launch streamit.bat, replacing %1 and %2 with the arguments specified, and the results will be displayed in the Console view. You can repeat this run by clicking the Run button with the red toolbox on the toolbar.

Related

How to export my program into a runnable desktop file?

first time posting here, java beginner.
I made a basic calculator that receives user input. Is there a way to export my program to make a runnable desktop file?
My IDE is IntelliJ.
Thanks in advance!!
Assuming you are building a JavaFX app:
The easiest way to deploy your app is to go to File>Project Structure>Artifacts.
Add your available elements (if you have any extra images etc) into your output root, and click on your jar file. At the bottom of the window you'll see options to either create a manifest file or modify an existing one.
The manifest file describes the first class to load in your program (the starting point) as well as the locations of any third party libraries you may have included in your program.
You will also have a JavaFX tab that you can use to set some initial parameters for your app, such as the title, version, and whether you want to deploy any native bundles (eg: .exe for Windows, .deb for Ubuntu etc)
Once you have configured the important parameters you want, save your settings (or just select any field and click enter) and go to Build>Build Artifacts>Action: Build
IntelliJ should generate an executable .jar file and any native bundles you selected. The native bundles can just be double clicked, and the jar file can be run using a JRE.
On blog.jetbrains.com they show the steps with some screenshots.
Note that this entire process is called deploying your app and there are a variety of tools and methods to do it. I just described what I believe is the simplest way.

IntelliJ Idea unable to execute java Program

I am trying to learn Java from an online class, however I am having problems following the instructor's video.
is his screen, where he is trying to print a line.
My screen looks like
As you can see, the "out" part of System.out is not the same on our screens, and the icon for class is different on both our screens as well. The main problem I am having is I'm not able to run the line of code that I wrote. I apologize if this question has been asked already, I was not sure what keywords I would use to search this problem.
As you can see, the "out" part of System.out is not the same on our screens
This probably is due to the fact that you haven't opened up a Java Project. As you can probably see in the Two Images, the instructor's image, has A Project hierarchy on the left, while yours is empty. Therefore, To correct this Try The Following Steps:
File->New Project->Java Project
Now follow the onscreen instructions to create the Java Project. Then the project would be created. Now expand the project on the left pane, and do it until you find a folder named src . Select the folder, and choose new Class, and then You can create your new Classes
The main problem I am having is I'm not able to run the line of code that I wrote.
This is probably due to the fact that you Have not set up your JDK path in IntellIJ. This is backed up by the fact, that your run button (The Green Arrow on the Instructor's), is grayed. To fix this, click On the small Button beside the arrow. There, Click On edit Configurations. The following Window should pop up
If Default is not expanded, then expand it and Choose Application. The Window Should probably be like this :-
Then Click On the plus icon on the top
There, Enter You Main Class(The Class Which has public static main void(String args[]))
If you can't, choose the ... option, and choose
Finally, select the JRE path, that is, the location where you installed The JRE.
Also, make sure that You have the 'path' environment variable set to the bin of your jdk Installation Folder.
Ask if you have any problems doing this.
First you shoud do this: File- New- Project... Then create new project. And then you click right mouse button on src folder(in your new project), choose new "Java class" and copy code to that new class. And then you'll be able to run your main method.
Problem is that you just use java-file from somewhere (desktop, probably), but you should have it inside your project.

RCP exported EXE shows java console

I've created a very simple RCP application (essentially just a default view).
I then created a product file and Exported the product (as many online guides have demonstrated). However when I run the .exe file a java console is shown alongside my application splash screen. Is this normal behaviour? (I wasn't expecting a console to show as it's been exported/deployed).
Current research suggests I should:
run with javaw.exe
use another installer
Any further insight or advice would be appreciated.
Look in the .product file at the 'Program Arguments' (on the 'Launching' tab in the editor). Remove the '-consoleLog' argument.

Despite creating file association, file will not open in my Application

In my registry, for the extension '.conf' I made the (Default) value point to the pathway of my App. However when I try clicking a file with the extension '.conf' the window asking the user to select a program still opens, rather than simply opening the app associated with the extension. Am I declaring something wrong?
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\.conf]
#="C:\\Users\\izhodzis\\workspace\\ConfCompiler\\src\\ConfCompiler.jar"
PS. The idea is that it will work on other peoples' computers. They shouldn't need to select "Always use the selected program to open this kind of file"
Your regedit should look something like this: PATH_TO_JAVAW -jar PATH_TO_CONFCOMPILER
"C:\\Program Files\\Java\\j2rex.y.z\\bin\\javaw.exe -jar C:\\Users\\izhodzis\\workspace\\ConfCompiler\\src\\ConfCompiler.jar"
That probably won't work but hopefully it would help you further.

Eclipse create java executable with external libraries

I've seen this topic in this forum but it I need a more basic explanation on how to do this.
I've done a program in Java with some external libraries (LWJGL and Slick).
So this is what I've done and my program won't start anyway, tell me where I've done wrong.
I have Eclipse 3.7.1
My project is opened in Eclipse and runs well in Eclipse
I click File -> Export
I select Java -> Runnable JAR file
Here I don't know what to choose in Launch configuration, when I click the dropdown I get the option to choose my main class so I do that.
I select an export destination
I select the option "Package required libraries into generated JAR" under Library Handling
I don't know what ANT script is so I don't use that
I click Finish
I copy my images-folder to the same location as the generate JAR-file
I try to start the JAR-file, something loads in the background but nothing happens, no window shows up, nothing.
I check the Task manager in windows and sees that a javaw.exe is running
What did I miss?
My program uses images for graphics like this:
image = new Image("images/filname.png");
I wonder if I need to change the paths before exporting or the method to load these?
Thanks!
If you want to get things from inside a jar file you need to have them in your classpath and access them as resources.
The constructor you use, refers to a physical file which cannot peek inside a jar-file.
(EDIT) Also note that you have no guarantee where the current working directory is. Hence any relative references may break, as you see.
Read this to learn how to use JarSplice to export your Eclipse project to a runnable jar-file. Regarding images, you can put them in the same directory as a class file (in the jar) and then writeSampleClass.class.getResourceAsStream("image.png")to retrieve an InputStream of the image. Then you can load it however you like.
I had the same problem and I was able to fix it. All I did was copy the data folder (which contains my resources) into the *.jar file. You can do this for example with WinRAR.

Categories

Resources