Import project to another workspace including run configurations - java

I'm moving a bunch of projects from one workspace to another.
Each of the projects had a main() function, which was called with some default arguments that was part of the Run configuration for that project.
When I import these projects, it looks like these run configurations are not copied. I need to specify the input arguments etc again - Run as -> Run Configurations -> update stuff .
Is there a way to copy them over also?

There may be an easier way, but if you find all the file named *.launch - that will give you the list, then just copy those files. We actually save the base set of launch files we use as part of our project so that they are part of the project. For launches created on-the-fly, you will find those in the workspace.

Open the menu "Run -> Run configurations ..." and select the Launch configuration you want to share inside your project. Goto the common tab inside the selected launch and there you can specify a "Save as" location via "Shared file:". As location select your project. So then an appropriate ".launch" file is created and can be checked in or exported with the project itself.

Related

How do I add .java/.class files to eclipse?

I know how to add .java/class files to a certain project, or import them into a package. So, when you're using eclipse you can import java.awt.; and java.util.; and the but those are all packages inside the JRE. So my real question is: is there any way to import a package containing the files i want into the JRE so that, if i create a new project, I can simply import them whenever I need them? Thanks!
Yes. Generally, you have more then one choice. You can link the project(s) in eclipse, and then have project B with a reference to project A. Or, you can create a build artifact like a jar file and add it to the project B build/run path. Simply add a jar file to your project, right click and select Add to Build Path. Another alternative is to use a build tool that manages dependencies like gradle, sbt, maven, or apache ivy. Note that there are a wide variety of third-party opensource libraries you can add as dependencies using these (and other) tools.
You seem to be asking how to create a shared library, which in Java is a .jar file. The steps to do this in Eclipse are:
In the Package Explorer, you can optionally pre-select one or more Java elements to export. (These will be automatically selected in the Opens the JAR export wizard JAR Package Specification wizard page, described in Step 4.)
Either from the context menu or from the menu bar's File menu, select Export.
Expand the Java node and select JAR file. Click Next.
In the JAR File Specification page, select the resources that you want to export in the Select the resources to export field.
Select the appropriate checkbox to specify whether you want to Export generated class files and resources or Export Java source files and resources. Note: Selected resources are exported in both cases.
In the Select the export destination field, either type or click Browse to select a location for the JAR file.
Select or clear the Compress the contents of the JAR file checkbox.
Select or clear the Overwrite existing files without warning checkbox. If you clear this checkbox, then you will be prompted to confirm the replacement of each file that will be overwritten.
Note: The overwrite option is applied when writing the JAR file, the JAR description, and the manifest file.
You have two options:
Click Finish to create the JAR file immediately.
Click Next to use the JAR Packaging Options page to set advanced options, create a JAR description, or change the default manifest.
Credit goes to the docs.

Unable to run Java code with Intellij IDEA

I have just downloaded the IDE, and I want to edit my first Java file with it, I'm not interested in creating a whole project, just editing the single file.
So I opened the file from my desktop with Intellij IDEA as I set it as my default program for opening .java files.
I write some code and the main run and debug buttons are greyed out! I can't run my code!
I have already installed Java 8 update 45 64-bit (I have a 64 bit OS) as well as the Java development kit (J8U45). I have set my global IDE SDK as my JDK installation, and when it prompts me I also set this as my project SDK, but still the run and debug buttons are unable to be used!
Edit: I am also unable to run my file regardless of if its in a project or not.
Edit 2: Screenshot of my project setup
Move your code inside of the src folder. Once it's there, it'll be compiled on-the-fly every time it's saved.
IntelliJ only recognizes files in specific locations as part of the project - namely, anything inside of a blue folder is specifically considered to be source code.
Also - while I can't see all of your source code - be sure that it's proper Java syntax, with a class declared the same as the file and that it has a main method (specifically public static void main(String[] args)). IntelliJ won't run code without a main method (rather, it can't - neither it nor Java would know where to start).
My classes contained a main() method yet I was unable to see the Run option. That option was enabled once I marked a folder containing my class files as a source folder:
Right click the folder containing your source
Select Mark Directory as → Test Source Root
Some of the classes in my folder don't have a main() method, but I still see a Run option for those.
right click on the "SRC folder", select "Mark directory as:, select "Resource Root".
Then Edit the run configuration. select Run, run, edit configuration, with the plus button add an application configuration, give it a name (could be any name), and in the main class write down the full name of the main java class for example, com.example.java.MaxValues.
you might also need to check file, project structure, project settings-project, give it a folder for the compiler output, preferably a separate folder, under the java folder,
Don't forget the String[] args in your main method. Otherwise, there's no option to run your program:
public static void main(String[] args) {
}
I had the similar issue and solved it by doing the below step.
Go to "Run" menu and "Edit configuration"
Click on add(+) icon and select Application from the list.
In configuration name your Main class: name of your main class.
Working Directory : It should point till the src folder of your project. C:\Users\name\Work\ProjectName\src
This is where I had issue and after correcting this, I could see the run option for that class.
Something else that worked for me:
Right click the folder in src containing your main
You'll see an option "run 'file.main()'" with the run icon.
Click it, and then the run icon in the top right and bottom left will turn green from then on.
Sometimes, patience is key.
I had the same problem with a java project with big node_modules / .m2 directories.
The indexing was very long so I paused it and it prevented me from using Run Configurations.
So I waited for the indexing to finish and only then I was able to run my main class.
If you can't run your correct program and you try all other answers.Click on Edit Configuration and just do following steps-:
Click on add icon and select Application from the list.
In configuration name your Main class: as your main class name.
Set working directory to your project directory.
Others: leave them default and click on apply.
Now you can run your program.enter image description here
Last resort option when nothing else seems to work: close and reopen IntelliJ.
My issue was with reverting a Git commit, which happened to change the Java SDK configured for the Project to a no longer installed version of the JDK. But fixing that back still didn't allow me to run the program. Restarting IntelliJ fixed this
-First Move Your Code Files in side the "src" Folder
-Make sure your Main method is declared like the following
public class Main {
public static void main(String []args){
}
}
then:
Go to Project configurations
select Java application,
check allow parallel run
and select your main class
and it should work
If you are just opened a new java project then create a new folder src/ in the man project location.
Then cut and paste all your package in that folder.
Then Right click on src directory and select option Mark Directory As > Sources Root.
If you use Maven, you must to declare your source and test folder in project directory.
For these, click F4 on Intellij Idea and Open Project Structure. Select your project name on the left panel and Mark As your "Source" and "Test" directory.

Can Eclipse have multiple output folders just like multiple source folders

I have an Eclipse project, which has two source folders - 'src', and 'test'. I would like the build from both these folders to go into separate output folders.
The reason being, both these folders have a property file called ServiceProviders.properties which has different values for production and test runs of the code.
The problem is, since there is only one output folder where all compiled classes are copied, the properties file in 'test' does not get copied, and as a result my test code gets the production version of the properties file. I guess I am trying to simulate the way we can have different classpaths in Maven (and their lookup order).
If this does not work, I will use a JVM property to specify certain values which I can use in the test case. However, it does not seem like an elegant solution.
Using Maven will get this to work, but I cannot use Maven in this project.
Update 1:
Thanks for both the answers. Since they were identical, I have accepted the one which was posted earlier.
Upadte 2:
Even though I could get multiple output folders for corresponding source folders, it did not solve my original problem which was to load a ServiceProviders.properties from test when I run test cases, and the one from src when I run the app on a server from Eclispe. The way I got it to work is by creating a separate jar file with ServiceProviders.properties file from the test folder. In the run configuration of my test cases, I add this jar file and change the order, so it appears as the first dependency for lookup.
Yes it can, i just have checked that.
If you go into Properties of your Project, and there to Java Build Path, where you choose your different Source-Folders, there also is an option called "Allow output folders for source folders", which, if checked adds an editable output-folder for each of your source-folders.
just select it and click the edit-button and you're fine
Easy: click "Allow output folders for source folders" on the Java Build Path | Source tab. Then, when you expand each folder's settings, you get "Output folder"
In the PackageExplorer view click on the source folder and select "Configure Output Folder..." You can now configure the output folder separately for each source folder.

How to create a project from existing source in Eclipse and then find it?

I have created several .java files. All of them are located in one directory. I used a text editor to write these files. Now I want to switch to Eclipse. How can I do it? I have tried many ways. None of them works.
ADDED:
I think the common way is to have source in the eclipse folder called "workspace". So, how do I get my files in these directory. Should I use eclipse to create a new project from existing source and Eclipse will put all file to the workspace? Or I should manually copy all my files to the workspace? Where should I put my class files than? Should I create a subdirectory? With which name?
ADDED 2:
When I try to create a project with name "game", the eclipse writes me that a project with such name already exist. But how can I open this project?
ADDED 3:
In my "workspace" I have created a subdirectory called "game". I copied all my .java file into this subdirectory. Then with Eclipse I created a new project with the name game. As a result, Eclipse created .classpath and .project files in the directory "game". It also created bin and src subdirectories. And now I think it is not the correct way to go. The source files are supposed to be in the "src" directory. Right? And at the moment all my .java files are in the "workspace\game".
ADDED 4:
I did it other way around. With Eclipse I have created a new project with the name "game". As the result, Eclipse created a folder called "game" into folder "workspace". In "game" the folder I found "src" folder. I copied all my .java files into this folder. But now in the "Package Explorer" I cannot open "src" folder. So, how can I access my source files from Eclipse? Why Eclipse does not want to open the "src" folder?
Easiest Method:
Put all source files into one directory named after your project. i.e. "ProjectName" You can keep this directory in your workspace or it can be somewhere else.
Start a new project in eclipse and name it using that same project name.
Uncheck the "use default location" box and find the directory where your project is unless your project is already in the workspace - then you must not uncheck the "use default location" box
Click 'next'.
Eclipse should be smart enough to figure out what's going on. After clicking next, it will show you all of the files it found in that directory. It will just automatically add those files to your project. Voilà!
Right-click in the package explorer and select New - Java Project
Create the new project Game
Open the new project in the package explorer - you should see only the source folder called src (there's nothing inside yet)
Open a file Explorer (e.g. Windows Explorer) and drag your sources
Drag them to Eclipse and drop them inside the new src folder - if asked select "Copy files"
Eclipse should put the files into the default package, if that's not correct you can edit the offending files (marked with a red cross) by opening them in Eclipse, selecting the package declaration (usually line 1), pressing Ctrl + 1 and selecting the appropriate option (e.g. "Move xy to package com.game"
This answer is going to be for the question
How to create a new eclipse project and add a folder or a new package into the project,
or how to build a new project for existing java files.
Create a new project from the menu
File->New-> Java Project
If you are going to add a new pakcage, then create the same package name here by
File->New-> Package
Click the name of the package in project navigator, and right click, and import...
Import->General->File system (choose your file or package)
this worked for me I hope it helps others.
Thank you.
The easiest method is really good but you don't get a standard Java project, i.e., the .java and .class files separated in different folders.
To get this very easily:
Create a folder called "ProjectName" on the workspace of Eclipse.
Copy or move your folder with the .java files to the "ProjectName" folder.
Create a new Java Project called "ProjectName" (with the Use default location marked).
Press <Enter> and that's it.
There are two things
1- If its already a Eclipse Project, then simply go to File->Import->General->Existing Project into Workplace
2- Otherwise define project type e.g. Java, Web etc
Create a new project of type you define into your workplace. Copy Paste source , lib and other necessary files. refresh, compile and run project in eclipse.
In the package explorer and the navigation screen you should now see the project you created. Note that eclipse will not copy your files, it will just allow you to use the existing source and edit it from eclipse.
There are several ways to add files to an existing Java project in Eclipse. So lets assume you have already created the Java project in Eclipse (e.g. using File -> New -> Project... - and select Java project).
To get Java files into the new project you can do any of the following. Note that there are other ways as well. The sequence is my preference.
Drag the files into the Navigator view directly from the native file manager. You must create any needed Java packages first. This method is best for a few files in an existing Java package.
Use File -> Import... - select File System. Here you can then select exactly which files to import into the new project and in which Java package to put them. This is extremely handy if you want to import many files or there are multiple Java packages.
Copy the fires directly to the folder/directory in the workspace and then use File -> Refresh to refresh the Eclipse view of the native system. Remember to select the new project before the refresh.
The last one is what you did - minus the refresh...
While creating a project from a full folder may or may not work within the workspace, there's a condition outside of the workspace that prevents starting a new project with a full folder.
This is relevant if you use numerous folder locations for sources, for example an htdocs or www folder for web projects, and a different location for desktop Java applications.
The condition mentioned occurs when Eclipse is told to create a new project, and given a full folder outside the workspace. Eclipse will say the folder isn't empty, and prevent creating a new project within the given folder. I haven't found a way around this, and any solution requires extra steps.
My favorite solution is as follows
Rename the full folder with an appended "Original" or "Backup.
Create the Eclipse project with the name of the full folder before the folder was renamed.
Copy all the relabeled full folders contents into the new project folder.
Eclipse should make a new project, and update that project with the new folder contents as it scans for changes. The existing sources are now part of the new project.
Although you had to perform three extra steps, you now have a backup with the original sources available, and are also able to use a copy of them in an existing project. If storage space is a concern, simply move/cut the source rather than fully copy the original folder contents.
If you creating a new project based on an existing Maven structure :
Create the project using a general project wizard and give the project the same name as just created.
If you try to create the project as a Maven project via m2e will receive an error that project/pom already exists.
Create a new project..
Right Click on your project..
Select Build path --> Configure Build Path
Under source tab choose link source, your .java files containing folder..
I am suggesting this since none of the methods that you tried have worked ---FYI
Follow this instructions from standard eclipse docs.
From the main menu bar, select command link File > Import.... The Import wizard opens.
Select General > Existing Project into Workspace and click Next.
Choose either Select root directory or Select archive file and click the associated Browse to locate the directory or file containing the projects.
Under Projects select the project or projects which you would like to import.
Click Finish to start the import.

How do I save Eclipse launch profiles across workspaces?

When I copy an Eclipse project directory, it contains the .classpath and .project files so that when I take that same directory to another Eclipse instance, I don't have to setup my build path and such (assuming that all the resources are contained in the project, and not external.)
However, this procedure doesn't cause launch profiles to "travel" with the directory.
Is there some other file/directory structure I can "carry around" to another instance of Eclipse that will include my launch profiles?
The .launch xml files (launcher definition) are found in
[eclipse-workspace]\.metadata\.plugins\org.eclipse.debug.core\.launches
Just copy them into your <project>/.settings directory.
Refresh your project, and there you have it: those launcher configuration are available, and you can copy them along the rest of your project files.
Warning: do uncheck the option "Delete configurations when associated resource is deleted" in the Run/Launching/Launch Configuration preference panel: it is common ot soft-delete a project in order to import it back again, to force a reinitialization of the eclipse metadata,... but this option, if checked, will removed your detailed launch parameters!
See also Which eclipse files belong under Version Control, and where are the external tools launch configurations in Eclipse.
Jared adds:
There is no .settings directory in my project directory - I created it and put the .launch file in there, refreshed my project, and it doesn't have any Run profiles. Do you have to mirror the directory structure under .metadata?
Just create the .settings directory (it is actually created when you have project specific settings as illustrated in the question "Maintaining a common set of Eclipse preferences").
Then delete the same .launch file from the .metadata directory.
Then refresh your project and go to the "Run Configurations" Panel: you will see your launch configurations still available, except they are stored where they belong: within your project directory and not buried under an obscure workspace metadata thingy.
Update June 2012
I always seen my launch configurations, saved under PROJECT_LOC/.settings, automatically imported when reloading the Eclipse workspace.
However, as described in "Tip: Creating and Sharing Launch Configurations (2007)", you should be able to reference a launch configuration saved into any subdirectory of your project if you go to the "Common" tab of your "Run Configurations" wizard.
So saving your launcher anywhere in the project won't display them automatically in the "Run Configurations" section of the preferences.
But declaring a new one, sharing its launcher as a "Shared file" (as illustrated above), and then overwriting that launcher with the actual one you had previously saved would update that run configuration.
Try choosing a shared file location inside your workspace from the "Common" tab of the launch configurations (profiles).
Depending on the amount of moving around you do and the number locations you move to, storing your launch files in an online storage space and then setting up folders at the new locations to synchronize with it may be a viable solution.
Dropbox
is a decent online storage system that gives users up to 2 gigabytes of storage.
Here is an example of how you can synchronize folders out side the Dropbox folder curtiousy of
LifeHacker.com :
Use either the JUNCTION utility from Sysinternals, or the MKLINK command built in to Windows Vista and Server 2008, for example:
junction "C:\Documents and Settings\User\My Documents\My Dropbox\DesiredFolder" "C:\Path\To\DesiredFolder"
mklink /D "C:\Users\Steve\Documents\Dropbox\DesiredFolder" "C:\Path\To\DesiredFolder"
Or, if you prefer a GUI, install Link Shell Extension.
[eclipse-workspace].metadata.plugins\org.eclipse.debug.core.launches
would be the DesiredFolder.
You could also use SyncToy to echo changes from another folder to your Dropbox folder. This keeps 2 copies on disk though.
On OS X or Linux, try the following:
Use the ln command, for example:
ln -s /path/to/desired-folder ~/Dropbox/desired-folder
This works with files too:
ln -s /path/to/desired-file ~/Dropbox/desired-file
Another easy way to do this with Terminal is type the ln -s part, then from Finder drag the folder/file that you want into the Terminal window then drag the Dropbox folder and hit return.
Note that an Alias file or folder does not work.
Another solution could be to keep your launch files in an svn repository. Search for How to run Eclipse launch configurations programmatically? on this site. Sorry can only post one url so I used it on Dropbox.
There is an another clean way to share the launch configurations in Eclipse.
Create a java project. For instance lets name it Launches-Configs.
Now create a new Run Configuration for a project that you need to
run.
On the Run Configurations window navigate to Common tab. Ensure
Shared File option is selected and input the Shared File location is set to
\Launches-Configs\src
Check in Launches-Configs to a VCS and share with other teams
I have had good use of creating a /launch folder, and then setting the launch configuration to be "Shared file:" (the Commons panel) to the above mentioned launch folder.
The resulting launch file can be checked into e.g. CVS git and be pulled into a fresh workspace.
Create a Launch Configuration with Shared File option selected under Common Tab.
This will create a configuration file with .launch extension.
Now in another workspace just go to Import and select Launch Configurations, choose the configurations from the directory and finish.
It will appear in the Run configuration now.

Categories